I'm creating a website using Node.js (Express Framework) and use Passportjs for authentication. I use socket.io for communicating between client and server. My website shows which users are online. All I want is that when client closes his tab or the browser itself without logging out of the website, I want the user to be logged out from the website when 'disconnect'
event fires. Code fragment should look like on server
side.
io.sockets.on('connection', function (socket) {
socket.on('disconnect', function() {
console.log("|||||| DISCONNECTED ||||||");
//LOGOUT USER CODE
});
});
I'm using Passportjs' local strategy. It searches for user in CouchDB database and logs in if Username/Password combination is correct.
My solution which obviously doesn't work:
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/'));
app.use(express.session({ secret: 'keyboard cat'}));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
//a new custom middleware to logout the user when the pathname is '/exit'
app.use(function(req, res, next) {
var pathname = url.parse(req.url).pathname;
if(pathname == '/exit')
req.logout();
next();
});
});
I used a node module called xmlhttprequest
defined like this to create http requests.
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
and then in my socket's disconnect event I added the following lines.
io.sockets.on('connection', function (socket) {
socket.on('disconnect', function() {
console.log("|||||| DISCONNECTED ||||||");
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:3000/exit");
xhr.send();
});
});
This doesn't work and the user is not logged out when disconnect
event fires. I can see this message |||||| DISCONNECTED ||||||
on console. Also if I enter http://localhost:3000/exit
in my browser address bar and then press enter, I see this message: Cannot GET /exit
in my browser but on pressing back and refreshing the page the user is logged out.
This code is no good.
app.use(function(req, res, next) {
var pathname = url.parse(req.url).pathname;
if(pathname == '/exit')
req.logout();
//try adding this line, so that it doesn't just show the "can't retrieve"
res.end('<html><head>logged out</head><body>logged out</body></html>');
next();
});
EDIT:
socket.on('disconnect', function() {
console.log("|||||| DISCONNECTED ||||||");
var xhr = new XMLHttpRequest();
//this won't work, don't do this
xhr.open("GET", "http://localhost:3000/exit");
xhr.send();
});
instead, do this:
socket.on('disconnect', function() {
console.log("|||||| DISCONNECTED ||||||");
user.logout();
});
I suggest doing a redirect from the page at this point, to exit, and have exit send another redirect to another page. It's how I would do it.