Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如果从新设备登录,则删除以前的会话_Node.js_Express_Mongoose_Meanjs_Express Session - Fatal编程技术网

Node.js 如果从新设备登录,则删除以前的会话

Node.js 如果从新设备登录,则删除以前的会话,node.js,express,mongoose,meanjs,express-session,Node.js,Express,Mongoose,Meanjs,Express Session,如果用户从新设备登录,我想从MongoStore/sessionStore中删除以前的会话。MEANJS正在使用 快速会议 连接mongo 在mongodb中存储会话。我确实搜索了它,但找不到从db获取会话列表的解决方案。请帮帮我 MongoStore = require('connect-mongo')(session), favicon = require('serve-favicon'), module.exports.initSession = function (app, d

如果用户从新设备登录,我想从MongoStore/sessionStore中删除以前的会话。MEANJS正在使用

快速会议 连接mongo

在mongodb中存储会话。我确实搜索了它,但找不到从db获取会话列表的解决方案。请帮帮我

MongoStore = require('connect-mongo')(session),
favicon = require('serve-favicon'),

    module.exports.initSession = function (app, db) {
  // Express MongoDB session storage
  app.use(session({
    saveUninitialized: true,
    resave: true,
    secret: config.sessionSecret,
    cookie: {
      maxAge: config.sessionCookie.maxAge,
      httpOnly: config.sessionCookie.httpOnly,
      secure: config.sessionCookie.secure && config.secure.ssl
    },
    key: config.sessionKey,
    store: new MongoStore({
      mongooseConnection: db.connection,
      collection: config.sessionCollection
    })
  }));
};

这应该行得通,至少对你来说是个好的起点

var async = require('async'); //npm install async --save
exports.removeSessionsForUser = function(req, res, next) {
    var userId = req.user ? req.user.id : undefined;
    if (!userId)
        return next(new Error('No user found in req. Exiting'));

    var store = req.sessionStore;
    var sessionsColl = store.db.collection('sessions');

    sessionsColl.find({
        'session.user': userId,
        // we are tryin to remove all sessions, you can leave current
        // '_id': { '$ne': req.sessionID }
    }, { _id : 1 }, function (err, userSessions) {
        async.each(userSessions, function (userSession, cb) {
            store.destroy(userSession._id, cb);
        }, function(notDone) {
            if(notDone)
                return next(new Error(notDone));

            res.send('ok');
        });
    });
}
这使用的是

async.each(Array, function(item, callback) {/* iterate */}, function(error) {/* end */});

最后,在我的帮助下,我将能够解决这个问题 这是我的密码

    exports.logoutFromPreviousDevices = function (req, res) {
  var userId = req.query.userid;
  if (!userId)
    return res.status(400).send({
      message: errorHandler.getErrorMessage('No user found in input request')
    });

  var store = req.sessionStore;
  var sessionsColl = store.db.collection('sessions');

  sessionsColl.find({
    // 'session.passport.user': userId,
    // we are tryin to remove all sessions, you can leave current
    // '_id': { '$ne': req.sessionID }
  }, function (err, userSessions) {
    if (userSessions !== null) {
      userSessions.toArray(function (a, sessionsData) {
        sessionsData.forEach(function (element, index) {
          var data = JSON.parse(element.session);
          if (element._id !== req.sessionID && req.query.userid === data.passport.user) {
            store.destroy(element._id, function (destroyerr, dat) {
              if (destroyerr)
                return res.status(400).send({
                  message: errorHandler.getErrorMessage(destroyerr)
                });
              res.jsonp({ status: 'Previous session deleted' });
            });
          }
        });
      });
    } else {
      res.jsonp({ status: 'No session found' });
    }
  });
};

如果你删除了公共会话,那么人们每次更换设备时都必须重新登录,使用公共会话,他们同时通过浏览器和手机登录time@MedetTleukabiluly好的,你能给我看一些指导代码吗?我应该在哪里删除公共会话代码?谢谢你的帮助,让我检查一下这个解决方案,然后再给你回复。谢谢again@ZaidIqbal注意:人们每次更改deviceuserSession时都必须重新登录。_id,_id未定义在此行中出错是的,这就是我需要的,人们每次更改设备时都需要重新登录。记录它,查看它有哪些字段,也许userSessions本身就是字符串化id,如['id1',id2']