2

I’m building a websocket middleware and I want to return a 401 from it.

This is my code:

async wsHandle({ request, response }, next) {


    let token =request.only('token').token;

    await axios.get('auth server url', {
      headers: {
        'Accept': 'application/json',
        'Authorization': token,
      },
    })
      .then(async (response) => {
        await next();
      })
      .catch(err => {
        if (err.response) {
          let err_code = err.response.status.toString()
          //This response can't return responses.
          response.status(err_code).send(err.response.data.message)
        }
      });
  }

Can anyone help me please?

Thanks.

Cristiano Casciotti
  • 1,016
  • 10
  • 21
Rick
  • 21
  • 3

1 Answers1

0

Question solved by Harminder Virk : https://forum.adonisjs.com/t/use-websocket-middleware-return-response-401/4473

Answer :

You cannot make standard response with HTTP status codes in websocket connections.

Simply throw an exception from the middleware and AdonisJs will send that error to the channel on('error') event listener on the client.

crbast
  • 2,192
  • 1
  • 11
  • 21