5

So I'm using express-session with a mongo store like so:

app.use(session({
  secret: 'some secret here',
  saveUninitialized: false,
  resave: false,
  store: new MongoStore({
    url: 'http://someurlhere'
  })
}));

I have some login middleware, which after a successful login I want to then set the session cookie expiry time.

So I am testing with a 10 second expiry time right now using

req.session.cookie.expires = new Date(Date.now() + 10000);

I want the session expiry to reset for each subsequent request. Currently after 10 seconds have elapsed, no matter how many requests I have made after logging in, the session expires.

I feel like I have misunderstood something here!

EDIT

Ok so I missed the rolling config option in the docs, but even when I set this to true in my session config options, the same behaviour occurs:

app.use(session({
  secret: 'some secret here',
  saveUninitialized: false,
  resave: false,
  store: new MongoStore({
    url: 'http://someurlhere'
  }),
    rolling: true,
    cookie: {
        maxAge: 10000
    }
}));

I am now console logging the value of the cookie maxAge across my routes and see it decreasing as each subsequent request is made after logging in, it never reset back to 10000.

What am I doing wrong?

SOLVED

Ok so I came across a comment on this issue

I changed resave to true and it works as expected now.

Arya
  • 2,140
  • 1
  • 9
  • 27
mindparse
  • 7,083
  • 17
  • 64
  • 146

0 Answers0