限制每个用户的会话 - ExpressJS

Limit session per User - ExpressJS

我正在使用 mongoDB 作为数据库和 passportjs 进行身份验证。

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

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

每次用户登录时,我都会增加计数,当他们注销时,我会减少。

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

问。有什么方法可以“检测”会话过期,以便我可以减少 sessionCount 字段?

    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();
    });