Brug af connect-mongo
, userId
er gemt inde i en streng i mongoDB i sessionssamling :
{
"_id" : "J6fsgZ4d1zKp31ml1MRm18YCdlyhvce-",
"session" : "{\"cookie\":{\"originalMaxAge\":15778475958,\"expires\":\"2016-05-17T23:47:27.301Z\",\"secure\":false,\"httpOnly\":true,\"path\":\"/\"},\"passport\":{\"user\":\"56420a5a8c6601ce29bbd1c1\"}}",
"expires" : ISODate("2016-05-17T12:48:22.049Z")
}
Til sidst bruger jeg denne kode til at fjerne alle hans sessioner:
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
Session = mongoose.model('Session', new Schema(), 'sessions');
exports.signoutAllSessions = function(req, res) {
var socketio = req.app.get('socketio');
var userId = req.user.id;
var filter = {'session':{'$regex': '.*"user":"'+userId+'".*'}};
req.logout();
res.redirect('/');
Session.remove(filter,function(err,data){
socketio.sockets.to(userId).emit('user.logout');
});
};
Og en API-rute kalder denne metode.