Replies: 4 comments 6 replies
-
You can use this little trick Ws.io.on('connection', async (socket) => {
socket.request.headers['authorization'] = socket.handshake.auth.token
const ctx = HttpContext.create('/', {}, socket.request)
const auth = AuthManager.getAuthForRequest(ctx as any)
try {
const user = await auth.use('api').authenticate()
socket.emit('user', user)
} catch (error) {
socket.disconnect()
}
}) P.S. Send token in handshake like this: 'Bearer {your token}' |
Beta Was this translation helpful? Give feedback.
-
how can i do this with adonis v6? how to access the AuthManager and HttpContext |
Beta Was this translation helpful? Give feedback.
-
folder:providers/file_name
and then inside my WebSocketIo class i get the app from the constructur check the config/auth file make sure to have the authConfig function like this
now inside the class WebSocketIo that got called in the provider and has the app class i did this
|
Beta Was this translation helpful? Give feedback.
-
i need to give it a read to understand how the incoding is done, imagin if you needed to decode this token in a java application or even another nodejs express application, there must be a simpler way |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am working on Adonis v5 for developing API for my mobile app.
And I am using WebSocket (socket.io), I need a way to authenticate the token I add to the socket handshake.
Why this feature is required (specific use-cases will be appreciated)?
In normal cases apart we only use the router to authenticate requests, it would be much nicer to just
call a function that can generate a user by passing in the token.
In the case of using Socket using this approach from the main Adonis Js documentation:
It would be much easier to authenticate a user in a socket file.
I don't know how to approach this but doing something like this from the code below really make sense to me :
Have you tried any other workarounds?
I tried to decode the Api Token but it seems not to be working.
Are you willing to work on it with little guidance?
Yes!, I will be much grateful for your help.
Beta Was this translation helpful? Give feedback.
All reactions