首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带有cookie的保留登录选项- express中的会话

带有cookie的保留登录选项- express中的会话
EN

Stack Overflow用户
提问于 2016-01-07 21:18:55
回答 2查看 2.7K关注 0票数 16

我希望有一个“留在登录”选项,如Gmail提供的选项。这样,用户就可以决定在先前关闭一个新的浏览器会话后,他们是否希望在打开该会话时保持该会话的打开状态。

查看github的问题时,我看到了the cookie-session component doesn't provide a way to upate the maxAge property dynamilly

那么,我想知道是否有任何方法可以通过cookie-session组件实现“保持登录”功能。

对我来说,这似乎是一个正在下载80K times a month的组件的基本功能。

EN

回答 2

Stack Overflow用户

发布于 2016-01-20 04:26:59

代码语言:javascript
复制
// 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
EN

Stack Overflow用户

发布于 2016-01-20 13:51:12

如果您使用的是ExpressJS,会话模块有一个选项。

https://github.com/expressjs/session

或者req.session.cookie.maxAge将返回以毫秒为单位的剩余时间,我们也可以重新分配一个新值来适当地调整.expires属性。下面是本质上等价的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34656168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档