Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 209
u_peerless Avatar asked Sep 12 '13 09:09

u_peerless


People also ask

Which property is used to set expiration time of cookies in express JS?

So, to set expire time to cookies, an object with expire property can be sent which holds the expire time in milliseconds. An alternate approach to set cookie expiration age is to use optional magAge property. res. cookie(name, 'value', {maxAge : 9999});

How do cookies expire in express?

cookie(name, 'value', {expire: 360000 + Date. now()}); Another way to set expiration time is using 'maxAge' property. Using this property, we can provide relative time instead of absolute time.

Does session cookie expire?

Session cookies expire once you log off or close the browser. They are only stored temporarily and are destroyed after leaving the page. They are also known as transient cookies, non-persistent cookies, or temporary cookies.

What happen if cookie expires max age is session?

Using cookies to do stuff Cookies without an Expires or Max-Age attribute are treated as session cookies, which means they are removed once the browser is closed. Setting a value on either Expires or Max-Age makes them permanent cookies, since they will exist until they hit their expiry date.


1 Answers

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)

like image 177
d2vid Avatar answered Sep 16 '22 15:09

d2vid