How can I find the session Id when using express / connect and a session store?

前端 未结 4 580
星月不相逢
星月不相逢 2021-01-31 16:55

If a user is already logged in and tries to login again in a new instance I\'d like it to log out the other user instance. I don\'t want the same user to be logged in twice on m

4条回答
  •  时光取名叫无心
    2021-01-31 17:07

    Store the SID with the account, when the user logs in during the database validation of the user account call .destroy(sid, fn), then update the SID in the database with the current SID of the user.

    In my case using MongoDB this is how i've done it:

    app.post('/login', function(req, res)
    {
      var sid = req.sessionID;
      var username = req.body.user;
      var password = req.body.pass;
    
      users.findOne({username : username, password : password}, function(err, result)
      { 
        ...
        sessionStore.destroy(result.session, function(){
           ...
           users.update({_id: result._id}, {$set:{"session" : sid}});
           ...
        }
        ...
      }
    }
    

提交回复
热议问题