问题
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