当我重新启动服务器时,我的会话结束并注销。这不会在常规页面刷新时发生。我正在使用 connect-mongo 来解决这个问题:
var session = require('express-session');
const MongoStore = require('connect-mongo')(session);
这是我用来存储会话的代码,重用了一个名为的现有 Mongo 连接thisDb
:
app.use(session({
secret: secretHash,
saveUninitialized: true,
resave: true,
secure: false,
store: new MongoStore({ db: thisDb })
}));
在成功登录期间:
var day = 60000*60*24;
req.session.expires = new Date(Date.now() + (30*day));
req.session.cookie.maxAge = (30*day);
在我的 Mongo shell 中,我可以验证登录时是否创建了一个新会话:
db.sessions.find()
{"cookie":{"originalMaxAge":2592000000,"expires":"2017-11-17T20:36:12.777Z","httpOnly":true,"path":"/"},"user":{"newNotifications":false,"username":"max","admin":"true","moderator":"true"},"expires":"2017-11-17T20:36:10.556Z"}