Websocket with Basic Auth

I have a problem with Websocket Authentification. I clone the repo from @virk https://github.com/thetutlage/adonis-websocket-auth

and change the authenticator to “basic”

Then i use the following html

<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.4.4/polyfill.min.js"></script>
    <script src="https://unpkg.com/@adonisjs/websocket-client"></script>
    <script>
      function startWebSocket() {
        ws = adonis.Ws("ws://127.0.0.1:3333").withBasicAuth('virk', 'secret').connect();

        ws.on("open", () => {
          subscribeToChannel();
        });

        ws.on("error", () => {
          console.log('error: open websocket')
        });
      }

      function subscribeToChannel() {
        const chat = ws.subscribe("chat");

        chat.on("error", () => {
          console.log("error: subscribe to channel chat");
        });

        chat.on("message", message => {
          console.log("message=" + message);
        });
      }

      startWebSocket()
    </script>
  </head>
  <body></body>
</html>

But every time a get an error:

{t: 4, d: {topic: “chat”, message: “E_MISSING_AUTH_HEADER: Cannot parse or read Basic auth header”}}

Whats wrong?

1 Like