node.js - 限制每个用户的会话 - 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();
});
推荐阅读
- amazon-web-services - AWS X-RAY 在同时获取跟踪摘要时显示奇怪的错误
- c++ - 在函数参数中区分数组和指针 - C++
- android - 为什么位置getTime滞后2-3分钟
- mysql - 无法在创建 MySQL 服务器的同一计划中使用 mysql
- c++ - 启动时 Visual Studio 程序窗口延迟
- r - 删除具有相同名称(不同值)的行
- c++ - 即使对象存在,读取也无效
- node.js - 从本地部署到 Heroku,然后从 Github 切换到 Heroku
- maven - 如何让 Artifactory 使用最新的(而不是最旧的)快照工件?
- python - 如果它们是 str/text 格式,如何比较 2 个日期时间值?