how to maintain persistent session in node js when server restarts? how to maintain persistent session in node js when server restarts? express express

how to maintain persistent session in node js when server restarts?


If you store session at external storage, then after restart it should be available.

Passport is not responsible for sessions. You setup session independently from passport in express. Passport is authentication middleware with strategy to use your session.you setup express session:

app.use(express.session(session options));

and after that you init and setup passport to use session:

app.use(passport.initialize());app.use(passport.session());

It means that regardless of whether you use passport or not, session configuration will be the same.

there are few ways to make sessions persistent:Most of them store session in db or in file system (memory storage is appropiate only in dev env). Please look at this npm search list link.

List of Compatible Session Stores from official express-session page https://github.com/expressjs/session#compatible-session-stores

Jwt token, if properly implemented, is stateless. It means that your server does not storage any session data, It doesnt know how many sessions are valid. It authorize request if it have valid jwt token.

Jwt token can store some data, like your user id. When your server receive token, it decode it and validate, then you have access to data from this token. Please read this article for more details :

https://stormpath.com/blog/jwt-the-right-way/

Most important parts (there are more important things, butthese are sometimes forgotten):

Always verify the signature before you trust any information in the JWT

and:

Do not contain any sensitive data in a JWT

Please look at this module for maintain jwt:

https://www.npmjs.com/package/json-web-token

or even for some hybrid solution module (redis session with jwt token):

https://www.npmjs.com/package/jwt-redis-session