-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
socket.request.user empty after v2 to v4 migration #5020
Comments
Hi! Were you able to find the culprit? |
@darrachequesne hi, thanks for the reply, no we didn't. We ended up preventing users from being logged in on different subdomains which is inconvenient for them. Would be very interested in any ideas you have. |
I see you are using Socket.IO middleware: for (const ns of [defaultNamespace, buildNamespace]) {
ns.use(wrap(sessionMiddleware));
ns.use(wrap(passport.initialize()));
ns.use(wrap(passport.session()));
} Did you try with function onlyForHandshake(middleware) {
return (req, res, next) => {
const isHandshake = req._query.sid === undefined;
if (isHandshake) {
middleware(req, res, next);
} else {
next();
}
};
}
io.engine.use(onlyForHandshake(sessionMiddleware));
io.engine.use(onlyForHandshake(passport.session()));
io.engine.use(
onlyForHandshake((req, res, next) => {
if (req.user) {
next();
} else {
res.writeHead(401);
res.end();
}
}),
); Reference: https://socket.io/how-to/use-with-passport |
@darrachequesne thanks for replying, I did try that along the way but there were a lot of moving parts so I will try it again and let you know if it changes anything. From memory that |
Describe the bug
Hi and thanks for this useful module. We are upgrading from v2 to v4 and have been struggling to get the behavior we had before, would love some input.
We have a Node.js/Angular app with many dynamic subdomains, and we have a separate Node.js websockets app. In v2 we did not specify the cookie domain or CORS origin and it all worked, with each subdomain getting its own subdomain-scoped cookie. This scope is important because users have different logins for different subdomains.
In v4 we haven't been able to achieve that same behavior. The closest we have come is if we specify the cookie domain and CORS origin as the domain (not the subdomain),
socket.request.user
is there. That's not a workable solution though, because that stops people being able to have different sessions for different subdomains.If we do not specify the cookie domain and CORS origin, the websocket connection succeeds but it does not contain
socket.request.user
.It works properly on localhost during development, it is only when subdomains are introduced that it breaks.
To Reproduce
Socket.IO server version:
4.5.1+
Websockets Server
Node.js server (where login happens)
Socket.IO client version:
4.5.1+
Client
Expected behavior
I expect
socket.request.user
to be populated as it was in v2Platform:
Additional information
To get from v2 to v4 there were some other related dependency changes. The list is:
ngx-socket-io
from3.4.0
to4.3.1
passport.socketio
replaced with the syntax from jfromaniello/passport.socketio#148 (comment)socket.io
from2.5.0
to4.5.1
(also tried4.7.5
)socket.io-client
from2.5.0
to4.5.1
(also tried4.7.5
)socket.io-redis
from5.4.0
to@socket.io/[email protected]
redis
from3.1.2
to4.6.13
Hopefully I shared all the relevant parts of code, lmk if I missed anything.
The text was updated successfully, but these errors were encountered: