passport: req.logout not working with "local strategy"
The req.logout
method doesn’t seem to delete the session values when invoked.
I’m using passport-local
and cookie-session
, with Express.
It looks like similar issues are discussed on StackOverflow.
Any ideas what might be causing the problem?
About this issue
- Original URL
- State: open
- Created 10 years ago
- Comments: 51
Yeah, just figured it out, some other post helped me out with this solution, and it worked for me in conjunction with the comment mentioned. Thanks @MichhDiego 😉
app.use(session({ ... resave: false, .... }));
Follow-up: for me, it appears to have been a dependency issue (possibly deriving from an automated refactoring that didn’t correctly exclude
node_modules
). I nuked my localnode_modules
directory, rannpm install
, and got a working session logout.Note that for my tests with
jasmine-node
andrequest
to work correctly, I had to make sure to supply arequest.jar()
instance in thejar
field of the request object.Here’s my logout:
This works fine with
express@4.10.1
,express-session@1.9.1
,passport@0.2.1
,passport-local@1.0.0
. I’m using theconnect-pg-simple
session store middleware with it, with no issues.I tried all of the solutions here and just couldn’t get anything to work. No matter what I did, I could not get the user logged out unless I manually deleted the
connect.gid
cookie.UNTILL
I changed my
/logout
route on the server-side from aPOST
to aDELETE
and then it started working. 🤷I had the same problem too, but the following workaround solved it:
// Express Session app.use(session({ secret: 'secret', saveUninitialized: false, resave: false, cookie: { maxAge: 1000 } }));
Then on the logout routerouter.get('/logout', function (req, res) { req.logOut(); // remove all session data req.session = null; res.redirect('/login'); });
Cheers.
app.get(‘/logout’,(req,res)=>{ req.logOut(()=>{ res.redirect(‘/login’) });
})
On you main app.js file you should configure passport before the auth routes (login, logout). Like this:
Before I had it the other way round and I kept getting the same error
This works:
app.get(‘/logout’, (req, res)=>{ res.clearCookie(‘Session_name’); res.redirect(‘/’); });
But, want to know why req.logout() is not deleteing session.
I came across similiar issue. I forgot to put { withCredentials: true } on client when axios request is send, which caused unexpected sessions behavior. After that, logout is working right, but don’t expect, that cookie will be deleted. Logout action just ‘invalidate’ the session in DB (user info will be empty)
So I am having these issues using Express 4.15.4, passport 0.4.0, passport-oauth2 1.4.0, cookie-session 1.3.1. Symptoms are the same as described above. I have:
My
/
path is protected - if the user is not logged in, it will redirect to login. I added this line to my middleware router:console.log(req.path, ':', req.isAuthenticated());
In Chrome, I navigated to/
, confirmed I was still logged in, then navigated to/logout
. I ended up back at/
logged in. But interestingly, this is how I got there:So it looks like (at least in my case), it is logging me out, but when forwarded to
passport.authenticate('oauth2')
, I’m automatically logged back in without prompt. Any thoughts? Is this symptom the same for everyone else here?