express-session Changes the session when the browser is closed

Server on express (4.16.3), with it the express-session (1.15.6) module works.

Code:

// ...
app.use(session({
  secret: 'mySecret',
  resave: false,
  saveUninitialized: true,
  store: new MongoDBStore({
    uri: 'my-url',
    collection: 'sessions'
  })
}))
// ...

The essence of the problem: I open the Yandex browser - assign a session, then close it and when I reopen it - a new session. The matter is that authorization is tied to sessions.

The problem is observed in the Yandex browser, microsoft EDGE and in all mobile browsers, while in chrome and opera works correctly.

Help solve the problem or maybe something can replace the module express-sessions

Upvotes: 1

Views: 1136

Answers (1)

feiiiiii
feiiiiii

Reputation: 1583

This is happening because your browser default expire the cookie when the browser is closed. In order to fix you can add cookie:{ maxAge: 60000} to your session.

app.use(session({
    secret: 'mySecret',
    resave: false,
    cookie:{ maxAge: 60000}, 
    saveUninitialized: true,
    store: new MongoDBStore({
       uri: 'my-url',
       collection: 'sessions'
 })

if you want to make the cookie to not expire, the best way is to set a large number.

// this will it expire in 200 years
cookie: { maxAge: 9000000000000}

or a very far future date in expire property.

// this will expire in year 9999 
cookie: {expires: new Date(253402300000000)}

Upvotes: 3

Related Questions