Reputation: 1341
I am using cookie session in express with Nodejs. I wanted the cookies to expire after 20 minutes of inactivity but the cookie expires after 20 minutes no matter how many request the user makes to the Nodejs server
I decreased the time to 10 seconds instead of 20 minutes so that it is easier to test:
app.use(cookieSession({
maxAge: 1 * 1 * 1 * 10000,
keys: [keys.session.secret] //HASH is an environemnt variable
}));
Is there a way to set the cookie to expire based on inactivity?
Upvotes: 0
Views: 930
Reputation: 695
create a middleware function and make the expiration time to 20 minutes in each request of the user.
app.use(function(req,res,next){
req.session._garbage = Date();
req.session.touch();
next()
})
So whenever the user sends a request expiry time will increase by 20 minutes.
Upvotes: 1
Reputation: 439
There is no direct way to do that. To make it work you can store jwt(json web token) in the cookie with the expiration of 20 minutes and can then check for 20 minutes rule. If it is not more than 20 minutes renew the token.
Upvotes: 0