Stay signed in option with cookie-session in express

房东的猫 提交于 2019-12-21 07:06:08

问题


I would like to have a "Stay signed in" option such as the one provided by Gmail. This way, the user can decide if they want to keep the session open upon opening a new browser session after previously closing it.

Looking into the github issues I saw the cookie-session component doesn't provide a way to upate the maxAge property dynamilly.

I'm wondering then if there's any way at all to achieve the "Stay signed in" feature with the cookie-session component.

It seems to me a basic feature for a component which is being downloaded 80K times a month.


回答1:


// This allows you to set req.session.maxAge to let certain sessions 
// have a different value than the default. 
app.use(function (req, res, next) {
  // here you can see whether they checked the checkbox or not, and change maxAge.
  // the default should be that it expires when the browser is closed
  req.sessionOptions.maxAge = req.session.maxAge || req.sessionOptions.maxAge

  // or you can try to set expires to 1 day from now:
  req.sessionOptions.expires = new Date(Date.now()+86400000)
  // or at the end of the session:
  req.sessionOptions.expires = 0
})



回答2:


If you are using ExpressJS, session module has an option.

https://github.com/expressjs/session

Alternatively req.session.cookie.maxAge will return the time remaining in milliseconds, which we may also re-assign a new value to adjust the .expires property appropriately. The following are essentially equivalent



来源:https://stackoverflow.com/questions/34656168/stay-signed-in-option-with-cookie-session-in-express

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