0

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 !

Xeway
  • 41
  • 7
  • I don't understand your comment "I can't set the MongoDB TTL to the session duration". Can you please elaborate? – barrypicker Dec 08 '21 at 17:21
  • @barrypicker yes sorry, when you store a session on MongoDB you can define when the data will last on the db (the TLL 'time to live'). I want that this duration last the session duration. – Xeway Dec 08 '21 at 19:00
  • Database sessions were introduced to MongoDB in version 3.6. By default a session can live for 30 minutes before they expire. After they expire a reaper process cleans up expired sessions every 5 minutes. While you can add database startup parameters to change these values it is recommended to keep the defaults. See https://docs.mongodb.com/manual/reference/parameters/#mongodb-parameter-param.localLogicalSessionTimeoutMinutes for more information. – barrypicker Dec 08 '21 at 21:01

0 Answers0