I'm created some passport authentication middleware for my socket.io/express app. It looks like:
io.use(function (socket, next) {
var data = cookie.parse(socket.handshake.headers.cookie);
var sessionID = cookieParser.signedCookie(data['connect.sid'], 'my balonga has a first name');
sessionStore.get(sessionID, function (err, data) {
if (err) next(err);
socket.handshake.passport = data.passport;
next();
});
});
It works great, but I have a namespace and it appears uses a different socket. Does this mean that I must reuse my middleware for each namespace?
I noticed that I connect to my namespace it calls the middleware for the base server and then the namespace, this means if I include the middleware in both places, I'm doing 2x the operations I need. Can I prevent this without removing the middleware at the base layer? None of these are app breakers, but it will change my architecture a bit and I'm concerned I'll have auth gaps at some point.
Summary:
- Must I reuse my middleware for each namespace?
- Can I prevent the default namespace middleware from being called without removing the middleware at the base layer, when it is a namespace being connected to?