Im creating a simple socket.io chat that uses passport.js for authentication. But everytime a user logout and login the old socket connection reconnects. This causes a problem because now there are two active connections to one page. So what I want to do is end the current connection when the user navigates to "/logout".
var socketNotLoaded = true;
var socket;
app.get('/', function(req, res) {
if (req.isAuthenticated()) {
if(socketNotLoaded) { //is true when new connection is required
require("./socket")(io,req.user) //runs the socket.io script
socketNotLoaded = false;
}
res.render('index', pug.get(req.user));
} else {
res.render('notloggedin', pug.get());
}
})
app.get('/logout', function(req, res) {
socketNotLoaded = true;
req.logout();
res.redirect('/');
})
Im using
- socket.io: 2.0.3
- passport: 0.4.0
- passport-local: 1.0.0
- express: 4.15.4
- express-session: 1.15.5
- cookie-parser: 1.4.3
- body-parser: 1.17.2
if(socketNoLoaded)call is using a module level variable to decide whether to initialize socket.io withreq.user. That just doesn't make sense. What do you expect to happen when the second user comes to your site? Or the third user? - jfriend00