How to reconnect with new JWT in handshake #4936
-
I have a case where I authenticate using a standard Bearer token strategy, with expirable JWT tokens. I use NestJS gateways server-side, and with authentication guards that check the handshake for permissions. Once the token expires, I disconnect the previous client and send a new connection with a new client. Is there any other way to update the connection auth, or trigger automatically another handshake, without reconnecting manually? Or is WS authentication meant to be validated only on initial connection? I have checked #3902, but this requires a reconnection. That reconnection either needs to be manual ( |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
It's up to you. You can either keep the existing connection alive with an expired token, or you can either force the reconnection, with:
socket.on("disconnect", (reason) => {
if (reason === "io server disconnect") {
socket.connect();
}
// automatic reconnection
}); See also: |
Beta Was this translation helpful? Give feedback.
It's up to you. You can either keep the existing connection alive with an expired token, or you can either force the reconnection, with:
socket.disconnect().connect();
on the client sidesocket.disconnect()
on the server side, and then you catch thedisconnect
event:See also: