balazs
balazs

Reputation: 5788

Why does my session cookie expire?

In node I have a https server, and I use sessions with a maxage value, here's the code:

app.use(express.cookieParser());
app.use(express.session({ 
  secret: 'secret stuff',
  store: sessionStore,
  cookie: { 
    secure: true,
    maxAge: 60 * 1000 //1 minute
  } 
}));

I want a functionality like if the user visits the site within maxAge period (1 min in this case) the cookie timer starts over again, and he/she has 1 minute left to loose it's session id.

I see that the req.session._expires is updated (by the session middleware), but the cookie is left as it is. So the cookie will expire, a new session id connect.sid will be generated.

How can I achieve this? I thought it's automatically done by the session middleware, but seems like the expiration of cookie and expiration of session are two different things, than what session._expires is it for?

Edit

Here DanielBaulig word my problem better. As Marc B wrote in the comments the cookie expires, so the session became orphaned. That's what I want to avoid, I want to renew the cookie, when the session is touched.

Upvotes: 3

Views: 3591

Answers (1)

Robert Peters
Robert Peters

Reputation: 4104

Try

 cookie: { 
    secure: true,
    expires: new Date(Date.now() + 60 * 1000), // plus 1 minute
    maxAge: 60 * 1000 //1 minute
 }

I'm thinking the expires Date isn't being reset properly.

Upvotes: 2

Related Questions