首页 > 解决方案 > 限制每个用户的会话 - ExpressJS

问题描述

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

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

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

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

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

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

标签: node.jsmongodbexpresspassport.jssession-cookies

解决方案


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

推荐阅读