Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript User Authentication With Express and Mongo Improving the App with Custom Middleware Using MongoDB as a Session Store

Peter Gess
Peter Gess
16,553 Points

Session is persisting in Mongodb even after logout

When I logout the session is still retrievable in Mongo through db.sessions.find(). It appears that when I login in again it is destroyed and replaced with a new one but it is not upon logout. My code for the route is below, any ideas?

router.get('/logout', function(req, res, next) {
  if (req.session) {
    req.session.destroy(function(err) {
      if (err) {
        return next(err)
      } else {
        return res.redirect('/')
      }
    })
  }
})
Tyler Armstrong
Tyler Armstrong
13,887 Points

Any luck figuring this out? I'm having the same issue and unable to log out.

Edit: If in Safari, empty your cache. Fixed it for me.

Hey, Peter. If you haven't already figured this out, post the rest of your files: -routes/index.js (the rest of it) -middleware/index.js -app.js maybe even your models/user.js

This route looks fine, so it'd be best see everything else you've made.

1 Answer

Peter Gess
Peter Gess
16,553 Points

Ahh, didn't think of clearing cache. That should work. There also are a number of commands that can kill sessions: https://docs.mongodb.com/manual/reference/server-sessions/