1

When I restart my server, my session ends and I am logged out. This does not happen on a regular page refresh. I am using connect-mongo to remedy this:

var session = require('express-session'); const MongoStore = require('connect-mongo')(session);

Here is the code I am using to store my session, reusing an existing Mongo connection called thisDb:

app.use(session({                                
        secret: secretHash,
        saveUninitialized: true,
        resave: true,
        secure: false,
        store: new MongoStore({ db: thisDb })
}));

During a successful log in:

var day = 60000*60*24;
req.session.expires = new Date(Date.now() + (30*day));          
req.session.cookie.maxAge = (30*day);  

In my Mongo shell, I can verify that a new session is created when I log in:

db.sessions.find()

{"cookie":{"originalMaxAge":2592000000,"expires":"2017-11-17T20:36:12.777Z","httpOnly":true,"path":"/"},"user":{"newNotifications":false,"username":"max","admin":"true","moderator":"true"},"expires":"2017-11-17T20:36:10.556Z"}

Max Pekarsky
  • 590
  • 2
  • 6
  • 20
  • when you restart your server? you mean when you terminate your nodejs app? can you show us what you get from `db.sessions.find()` when you shut down the server and run it again? – Ahmed Can Unbay Oct 18 '17 at 21:25
  • @turmuka - yes, when I terminate/restart my node app, or make any changes to my code, since I'm using Nodemon which restarts the server for me. Here are two `db.sessions.find()` calls, the first while I'm logged in, and the second after restarting the server (which logs me out): – Max Pekarsky Oct 18 '17 at 21:31
  • > db.sessions.find()[0] { "_id" : "ZFGZJL9yc2Z3D7eI-SI7pifnyvVbmSLl", "session" : "{\"cookie\":{\"originalMaxAge\":2592000000,\"expires\":\"2017-11-17T21:29:21.872Z\",\"httpOnly\":true,\"path\":\"/\"},\"user\":{\"newNotifications\":false,\"username\":\"max\",\"admin\":\"true\",\"moderator\":\"true\"},\"expires\":\"2017-11-17T21:29:21.845Z\"}", "expires" : ISODate("2017-11-17T21:29:21.872Z") } – Max Pekarsky Oct 18 '17 at 21:31
  • > db.sessions.find()[0] { "_id" : "ZFGZJL9yc2Z3D7eI-SI7pifnyvVbmSLl", "session" : "{\"cookie\":{\"originalMaxAge\":2592000000,\"expires\":\"2017-11-17T21:29:21.872Z\",\"httpOnly\":true,\"path\":\"/\"},\"user\":{\"newNotifications\":false,\"username\":\"max\",\"admin\":\"true\",\"moderator\":\"true\"},\"expires\":\"2017-11-17T21:29:21.845Z\"}", "expires" : ISODate("2017-11-17T21:29:21.872Z") } – Max Pekarsky Oct 18 '17 at 21:31
  • I don't really know. Maybe your server sends your client a message at exit that it will no longer exist, and your browser deletes the cookies? maybe you should check the cookies on your browser. Also try to put those extra information in your question and not here. Please edit it next time. – Ahmed Can Unbay Oct 18 '17 at 21:37

1 Answers1

0

Well, almost 3 years later i was having this issue. Don't know if OP was using Passport but i resolved this issue by moving this functions from inside the passport.use function to outside:

passport.serializeUser(function(user, done){
    done(null,user.id);
});

passport.deserializeUser((id,done) => {
    User.findById(id, (err,user) => {
        done(null,user);
    });
});
Jaime
  • 11
  • 3