user203617
user203617

Reputation: 533

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.

Upvotes: 7

Views: 10128

Answers (3)

Mukul Tijare
Mukul Tijare

Reputation: 1

I know it too late to answer on this query but may it will help someone else.

const session = require('express-session');
app.use(session({
secret: 'secret',
name: 'farmerApp',
resave: true,
saveUninitialized: true,
cookie: {
    expires: 120000
}
}));

Upvotes: 0

TheLovelySausage
TheLovelySausage

Reputation: 4094

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
        }
    })
);

Upvotes: 6

srquinn
srquinn

Reputation: 10481

This is a bug in connect. See this issue:

https://github.com/senchalabs/connect/issues/670

Upvotes: 0

Related Questions