Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set individual session maxAge in express?

I understand that you can set the maxAge when starting up the app as follows:

connect.session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}) 

However, i would like to implement something along the lines of "remember me" setting, how would i go about doing that?

Thanks a lot :)

Jason

like image 918
FurtiveFelon Avatar asked Mar 15 '12 06:03

FurtiveFelon


People also ask

How do you set an express session cookie?

var cookieSession = require('cookie-session') var express = require('express') var app = express() app. use(cookieSession({ name: 'session', keys: ['key1', 'key2'] })) // Update a value in the cookie so that the set-cookie will be sent. // Only changes every minute so that it's not sent with every request. app.

How do I handle multiple sessions in node JS?

Here, since sess is global, the session won't work for multiple users as the server will create the same session for all the users. This can be solved by using what is called a session store. We have to store every session in the store so that each one will belong to only a single user.

What is saveUninitialized in Express session?

saveUninitialized : When an empty session object is created and no properties are set, it is the uninitialized state. So, setting saveUninitialized to false will not save the session if it is not modified. The default value of both resave and saveUninitialized is true, but using the default is deprecated.

How do I store session data Express?

We can use the express-session package to keep session cookie data on the server-side. There're many options like the content of various cookie attributes and the time to expiry. Other settings like the ID, whether to save cookie only in HTTPS and so on can be set. The cookies will be stored in a session store.


1 Answers

You can set either expires or maxAge on the individual cookie belonging to the current user:

// This user should log in again after restarting the browser req.session.cookie.expires = false;  // This user won't have to log in for a year req.session.cookie.maxAge = 365 * 24 * 60 * 60 * 1000; 

See connect session documentation.

like image 123
Linus Thiel Avatar answered Oct 02 '22 13:10

Linus Thiel