express-session: Rolling session expiry configuration

谁说胖子不能爱 提交于 2019-12-07 16:39:36

问题


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 - https://github.com/expressjs/session/issues/189#issuecomment-182631933

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

来源:https://stackoverflow.com/questions/36352179/express-session-rolling-session-expiry-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!