3

I am trying to incorporate the Passport logout function into one of my get-request routes for express and it doesn't seem to be destroying the session. After I make a logout request the endpoint that I am trying to hide is still accessible.

Here is the code:

const express = require('express');
const router = express.Router();
const app = express();
const path = require('path');
const passport = require('passport');
const passportHttp = require('passport-http');
const logout = require('express-passport-logout');

router.get('/', function (req, res) {
    logout();
    console.log('logged out');
    res.sendFile(path.resolve('./public/logout.html'));
})

module.exports = router;

Any help would be appreciated! Thanks!

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Jeremy
  • 79
  • 1
  • 1
  • 6
  • sorry guys I forgot to add that I have tried all of the spelling variations in your examples and in the passport docs but still nothing. – Jeremy Aug 10 '17 at 22:29
  • also I have read that post already it didn't solve my issue, as the recommended solution is deprecated. – Jeremy Aug 10 '17 at 22:47
  • Could you give us some more information if the suggested solutions don't work? You could tell us what versions you're using, what other packages you're using etc. Also plz read my edited answer – Singulasar Aug 12 '17 at 18:28
  • I have tried lots of solutions but none of them worked for me. Finally, I tried to update package passport@0.2.0 to passport@0.2.2 and it works! – prisan Apr 08 '18 at 02:05

4 Answers4

3

Have you tried using req.logout(); instead of logout();

without these 2 packages?

const passportHttp = require('passport-http');

const logout = require('express-passport-logout');

Singulasar
  • 76
  • 6
2

The given module actually returns a router handler i.e. function (req, res) {..}. Which in your case does not take req. You can see the source code here.

You can use it like this:

router.get('/logout', logout());

You can use a module like following as middleware:

var logout = function() {
    return function (req, res, next) {
        req.logout();
        delete req.session;
        next();
    };
 };

 router.get('/', logout, function (req, res) {
     console.log('logged out');
     res.sendFile(path.resolve('./public/logout.html'));
 })
Tolsee
  • 1,695
  • 14
  • 23
1

If you are using express locals, then after req.logout(), add this line:

req.user=null

or

delete req.user

I hope this helps

lifeisfoo
  • 15,478
  • 6
  • 74
  • 115
0
/* Handle Logout */
router.get('/logout', function(req, res) {
    console.log("I am Logout")
    req.logout(); 
    res.json({ 
            status: "logout",
            msg:"Please Log In again"
         });
});

This will work for sure as far as you don't have any mistake at frontend.

Aditya Parmar
  • 1,139
  • 13
  • 22