How to maintain a persistent session in node js when restarting the server?

As far as I have studied so far, stackoverflow answers on how to make sessions persistent after a server reboot.

There are 4 possible ways that I deal with my average application.

Now, I doubt that I will restart my server in mongo and redis. the session will continue to be present as they are external data stores. but how to make the session persistent using JWT and cookie sessions. where are these session variables stored.

In the case of pass.js, the solution I came across is to make the session persistent - store the session data in connect-mongo / connect-redis.

Is there any other way in the passport to make sessions permanent?

+7
session express redis
source share
1 answer

If you store the session in external storage, then after restarting it should be available.

Passport is not responsible for the sessions. You set up a session regardless of the passport in express. A passport is an authentication middleware that uses a session use strategy. you are setting up an express session:

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

after which you start and set the passport to use the session:

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

This means that whether you use a passport or not, the session configuration will be the same.

There are several ways to make sessions persistent: Most of them store the session in db or in the file system (memory is stored only in dev env). Have a look at this npm link search list.

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

The Jwt icon, if properly implemented, is stateless. This means that your server does not store session data, it does not know how many sessions are valid. It resolves the request if it has a valid jwt token.

The Jwt icon may store some data, such as your user ID. When your server receives a token, it decodes it and checks, then you have access to data from this token. Please read this article for more details:

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

The most important parts (there are more important things, but sometimes they are forgotten):

Always verify your signature before trusting any information in the JWT

and

Do not contain sensitive data in JWT

Please see this module for jwt support:

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

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

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

+5
source share

All Articles