如果用户从新设备登录,我想从 MongoStore/sessionStore 中删除以前的 session 。 MEANJS 正在使用
express-session connect-mongo
将 session 存储在 mongodb 中。我确实搜索了它,但找不到从数据库获取 session 列表的解决方案。请在这里帮助我
MongoStore = require('connect-mongo')(session),
favicon = require('serve-favicon'),
module.exports.initSession = function (app, db) {
// Express MongoDB session storage
app.use(session({
saveUninitialized: true,
resave: true,
secret: config.sessionSecret,
cookie: {
maxAge: config.sessionCookie.maxAge,
httpOnly: config.sessionCookie.httpOnly,
secure: config.sessionCookie.secure && config.secure.ssl
},
key: config.sessionKey,
store: new MongoStore({
mongooseConnection: db.connection,
collection: config.sessionCollection
})
}));
};
最佳答案
这应该可行,至少对你来说是一个很好的起点
var async = require('async'); //npm install async --save
exports.removeSessionsForUser = function(req, res, next) {
var userId = req.user ? req.user.id : undefined;
if (!userId)
return next(new Error('No user found in req. Exiting'));
var store = req.sessionStore;
var sessionsColl = store.db.collection('sessions');
sessionsColl.find({
'session.user': userId,
// we are tryin to remove all sessions, you can leave current
// '_id': { '$ne': req.sessionID }
}, { _id : 1 }, function (err, userSessions) {
async.each(userSessions, function (userSession, cb) {
store.destroy(userSession._id, cb);
}, function(notDone) {
if(notDone)
return next(new Error(notDone));
res.send('ok');
});
});
}
这使用 async.each
,即
async.each(Array, function(item, callback) {/* iterate */}, function(error) {/* end */});
关于node.js - 如果从新设备 MEANJS 登录,请删除以前的 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40720772/