Express (4.16.3) 上的服务器,使用它 express-session (1.15.6) 模块可以工作。
代码:
// ...
app.use(session({
secret: 'mySecret',
resave: false,
saveUninitialized: true,
store: new MongoDBStore({
uri: 'my-url',
collection: 'sessions'
})
}))
// ...
问题的本质:我打开 Yandex 浏览器 - 分配一个会话,然后关闭它,当我重新打开它时 - 一个新的会话。问题是授权与会话相关联。
该问题在 Yandex 浏览器、microsoft EDGE 和所有移动浏览器中都可以观察到,而在 chrome 和 opera 中可以正常工作。
帮助解决问题或者可能有什么东西可以代替 express-session 模块
发生这种情况是因为您的浏览器默认会在浏览器关闭时使 cookie 过期。为了修复您可以添加 cookie:{ maxAge: 60000} 到您的会话。
app.use(session({
secret: 'mySecret',
resave: false,
cookie:{ maxAge: 60000},
saveUninitialized: true,
store: new MongoDBStore({
uri: 'my-url',
collection: 'sessions'
})
如果想让cookie不过期,最好的办法就是设置一个大的数字。
// this will it expire in 200 years
cookie: { maxAge: 9000000000000}
或到期财产中的很远未来日期。
// this will expire in year 9999
cookie: {expires: new Date(253402300000000)}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句