I am trying to set the expiry time of cookie-session
dynamically based on the value pulled from the database. For some users the value might be less or more than other user.
When the server initializes this is how I am setting up my cookie-session:
server.use(require('cookie-session')({
name: 'session',
secret: keys.session.secret,
maxAge: 1 * 10 * 60 * 1000,
}));
I wanted to reset the cookie based on inactivity so this code resets the expiry date to maxAge value based on activity.
server.use(async (req, res, next) => {
try {
// Reset inactivity
// 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.
req.session.nowInMinutes = Math.floor(Date.now() / 60e3); //
....
So based on some research online, I saw that I should be able to reset the value of the expiry doing something like this:
// 30 seconds is just a test. I will pull the expiry value from the database based // on
// user ID but for now if a login is detected change the expiry date to 30 seconds
//
if (req.user) {
// Reset max age of cookie to 30 seconds
res.cookie('cookieName', 'value', { maxAge: 30 * 1000 });
}
This does not seem to work. The session value in browser still shows 10 minutes after current time:
How would I dynamically set the expiry value for a a user cookie ideally only once.