u_peerless
u_peerless

Reputation: 634

nodejs, how to set express session cookie not to expire anytime

In express, does setting maxAge=null in session cookie, sets the session cookie not to expire for life time?

Upvotes: 17

Views: 17737

Answers (2)

d2vid
d2vid

Reputation: 2302

If you don't want the session to expire, set the cookie expires date to a date far in the future:

app.use(session({
  store: sessionStore, 
  secret: config.session.secret, 
  cookie: {expires: new Date(253402300000000)}  // Approximately Friday, 31 Dec 9999 23:59:59 GMT
}))  

See the express session docs.

If you're concerned about using a date so far in the future, try new Date(2147483647000) (Tue, 19 Jan 2038 03:14:07 GMT, which is 2^31 - 1 in milliseconds)

Upvotes: 20

max
max

Reputation: 6069

Express is using Connect's session middleware to handle that:

By default cookie.maxAge is null, meaning no "expires" parameter is set so the cookie becomes a browser-session cookie. When the user closes the browser the cookie (and session) will be removed.

Upvotes: 8

Related Questions