Note that there are some explanatory texts on larger screens.

plurals
  1. PONode.js/Socket.io/Passport.js LOGOUT issue
    primarykey
    data
    text
    <p>I'm creating a website using <a href="http://nodejs.org/" rel="nofollow">Node.js</a> (<a href="http://expressjs.com/" rel="nofollow">Express</a> Framework) and use <a href="http://passportjs.org/" rel="nofollow">Passportjs</a> for authentication. I use <a href="http://socket.io/" rel="nofollow">socket.io</a> 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 <code>'disconnect'</code> event fires. Code fragment should look like on <code>server</code> side.</p> <pre><code>io.sockets.on('connection', function (socket) { socket.on('disconnect', function() { console.log("|||||| DISCONNECTED ||||||"); //LOGOUT USER CODE }); }); </code></pre> <p>I'm using Passportjs' local strategy. It searches for user in CouchDB database and logs in if Username/Password combination is correct.</p> <p>My solution which obviously doesn't work:</p> <pre><code>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(); }); }); </code></pre> <p>I used a node module called <code>xmlhttprequest</code> defined like this to create http requests.</p> <pre><code>var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; </code></pre> <p>and then in my socket's disconnect event I added the following lines.</p> <pre><code>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(); }); }); </code></pre> <p>This doesn't work and the user is not logged out when <code>disconnect</code> event fires. I can see this message <code>|||||| DISCONNECTED ||||||</code> on console. Also if I enter <code>http://localhost:3000/exit</code> in my browser address bar and then press enter, I see this message: <code>Cannot GET /exit</code> in my browser but on pressing back and refreshing the page the user is logged out.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload