New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow socket.handshake.query to be a function #1118
Comments
+1 |
@reeltimedoktor currently you should be able to update the socket.on('reconnect_attempt', function () {
socket.io.opts.query = { token: /* ... */ };
}); |
@darrachequesne 's solution doesn't seem to work for me :( I'm on On the client I'm doing this: socket.on('reconnect_attempt', async () => {
const newToken = await getNewToken();
socket.io.opts.query = {
token: latestToken,
};
}); And then the server is authenticating the token using a middleware passed to At the moment the server auth is failing on reconnecting, saying the token has expired - it seems the new token isn't being properly assigned to Thanks :) |
Update: Seems like there are a bunch of different places in the socket object where the auth token is stored, e.g. The token also seems to be included as a URL parameter in some of the url fields in the socket object. Perhaps these need to be updated too? |
After investigating further, it seems that setting the token on |
Ok, think I've fixed it - you need to make sure you are getting the new token synchronously in the 'reconnect_attempt' callback, i.e. don't do this: socket.on('reconnect_attempt', async () => {
const newToken = await getNewToken(); // won't work
socket.io.opts.query = {
token: latestToken,
};
}); It seems like the server side authentication was being done before the new token had been set. If you get the token synchronously (by getting it some time previously and storing it, and then just retrieving it in the listener callback), the server gets the new auth token in time for reconnection authentication. |
We having a similar issue. But my current workaround is to |
For future readers: The behavior of the
So I'm not sure how we could fix it in a backward-compatible way... Please note that this is fixed in Socket.IO v3, as you can now use the // plain object
const socket = io({
auth: {
token: "abc"
}
});
// or with a function
const socket = io({
auth: (cb) => {
cb({
token: "abc"
});
}
}); It should work with an const socket = io({
auth: async (cb) => {
cb({
token: "abc"
});
}
}); See also: |
You want to:
Current behaviour
socket.handshake.query
currently allows data to be set onconnect
, but cannot be updated by reference beforereconnect
is fired. This causes trouble when authorizing a socket connection with refresh tokens.Steps to reproduce (if the current behaviour is a bug)
If the server is restarted and clients
reconnect
, the token might be outdated when re-authorizing the socket.Expected behaviour
Allow query to be a
function
:Setup
How to fix
In current Socket constructor:
Can be changed to this:
The text was updated successfully, but these errors were encountered: