【问题标题】:Limit session per User - ExpressJS限制每个用户的会话 - ExpressJS
【发布时间】:2020-08-05 17:58:53
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: node.js mongodb express passport.js session-cookies


    【解决方案1】:
        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();
        });
    

    【讨论】:

      猜你喜欢
      • 2018-10-06
      • 1970-01-01
      • 2011-10-27
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      相关资源
      最近更新 更多