限制每个用户的会话-ExpressJS



我使用mongoDB作为数据库,并使用passportjs进行身份验证。

我希望每个用户最多有3个会话。

为此,我在mongo文档上创建了一个sessionCount字段。

每次用户登录时,我递增计数,当他们注销时,我递减计数。

但当会话自动过期时,问题就出现了。会话计数保持不变。

Q。有什么办法";"检测";会话过期,以便我可以减少会话计数字段?

I am also facing the same kind of issue I solve it by using this way->


app.use(function(req, res, next) {
//Checking previously set cookie (if there is one)
var session = JSON.parse(req.cookies['session'] || '');
if (session && new Date(session.expires) < new Date()) {
// decrement the count
}
next();
});

最新更新