I'm writing a node express application which offers some interaction between users by mean of socketio. There is a login page, and I'm using passport to handle the session. I'm importing the passport session into socket.io using:
const io = require('socket.io')(https)
io.use((socket,next) => sessionMware(socket.request, socket.request.res, bext)
where sessionMware is defined as:
var sessionMware = session({
genid:(req) => {
return Date.now() + "_" + uuid()
},
store: new FileStore(),
secret: 'not_whatever',
resave: false,
saveUninitialized: true,
name: 'mis_id',
})
then inside the onConnection function, which is called as the application gets a socket connection (io.sockets.on('connection', onConnection)), I access socket.request.session.passport.user to get the username of the connected client.
The solution works, but since I'm still debugging the application I'm often restarting the server in a local environment, and every now and then socket.request.session.passport.user returns undefined and consequently the application crashes.
I can't really figure out how to reproduce the bug exactly, but in the mean time I was wondering:
QUESTION Is there any way to handle this bug in such a way that:
function onConnection(socket){
if(socket.request.session.passport.user == 'undefined'){
// ROUTE THE CLIENT TO LOGIN PAGE...?
}
}
Please let me know if you need specific details about the structure of my code...I was just trying to highlight the issue. I'll be happy to edit my post. Thanks!