Change TTL connect-mongo expiration option to session time

493 Views Asked by At

I'm trying to store sessions into MongoDB. The problem I'm facing is that I can't set the MongoDB TTL to the session duration. Here's my code :

app.use(session({
    secret: md5("test"),
    resave: false,
    saveUninitialized: true,
    store: MongoStore.create({ mongoUrl: 'mongodb://localhost/test-db' }),
    cookie: { secure: false }
}));

If I don't store the session inside a database, it's simple because I have to remove the store key and as the maxAge of my cookie is not defined, it will last while the user hasn't closed the browser. But if I store inside MongoDB, as maxAge or expires is not defined, it will set by default an expiration of 14 days which I don't want. It's really annoying because if a restart my browser I will have multiples sessions for only one client.

So do you have a solution please ?

Thanks !

0

There are 0 best solutions below