Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session timeout after inactivity in express-session in Express server

I want to expire the session after 1 min of inactivity. I tried the following

 app.use(session({
     secret: config.sessionSecret,
     cookie: { maxAge: 60000 },
     store: new mongoStore({
        db: db.connection.db,
        collection: config.sessionCollection
     })
 }));

The session is expiring exactly after 1 min the user logs in even if the user continues to use the application. I have been unable to set it to expire after 1 min of inactivity. Any ideas? Thanks in advance again.

like image 270
user203617 Avatar asked Oct 01 '22 20:10

user203617


1 Answers

I'm not sure if you've resolved your issue but I had the same issue and I'd like to post what fixed it here in case it can benefit anyone.

It seems that the default expires will end the session after the specified length of time regardless of what the user is doing but if you include resave and rolling variables it will only expire after it has been idle for the specified length of time.

var express = require("express");
var session = require("express-session");

var app = express();

app.use (
    session ({
        secret: "53cr3t50m3th1ng",
        resave: true,
        rolling: true,
        saveUninitialized: false,
        cookie: {
            expires: 30 * 1000
        }
    })
);
like image 77
TheLovelySausage Avatar answered Oct 31 '22 17:10

TheLovelySausage