diff --git a/services/web/app/coffee/Features/Authentication/AuthenticationController.coffee b/services/web/app/coffee/Features/Authentication/AuthenticationController.coffee index e7db5d9f65..8851363b5f 100644 --- a/services/web/app/coffee/Features/Authentication/AuthenticationController.coffee +++ b/services/web/app/coffee/Features/Authentication/AuthenticationController.coffee @@ -9,6 +9,7 @@ Url = require("url") Settings = require "settings-sharelatex" basicAuth = require('basic-auth-connect') UserHandler = require("../User/UserHandler") +UserSessionsManager = require("../User/UserSessionsManager") module.exports = AuthenticationController = login: (req, res, next = (error) ->) -> @@ -141,4 +142,6 @@ module.exports = AuthenticationController = req.session[key] = value req.session.user = lightUser + + UserSessionsManager.onLogin(user, req.sessionID, () ->) callback() diff --git a/services/web/app/coffee/Features/User/UserController.coffee b/services/web/app/coffee/Features/User/UserController.coffee index 5dd32d1978..3b7bbb5d29 100644 --- a/services/web/app/coffee/Features/User/UserController.coffee +++ b/services/web/app/coffee/Features/User/UserController.coffee @@ -8,6 +8,7 @@ logger = require("logger-sharelatex") metrics = require("../../infrastructure/Metrics") Url = require("url") AuthenticationManager = require("../Authentication/AuthenticationManager") +UserSessionsManager = require("./UserSessionsManager") UserUpdater = require("./UserUpdater") settings = require "settings-sharelatex" @@ -81,9 +82,12 @@ module.exports = UserController = logout : (req, res)-> metrics.inc "user.logout" logger.log user: req?.session?.user, "logging out" + sessionId = "#{req.sessionID}" + user = req?.session?.user req.session.destroy (err)-> if err logger.err err: err, 'error destorying session' + UserSessionsManager.onLogout(user, sessionId) res.redirect '/login' register : (req, res, next = (error) ->)-> diff --git a/services/web/app/coffee/Features/User/UserSessionsManager.coffee b/services/web/app/coffee/Features/User/UserSessionsManager.coffee new file mode 100644 index 0000000000..2cc5d73821 --- /dev/null +++ b/services/web/app/coffee/Features/User/UserSessionsManager.coffee @@ -0,0 +1,31 @@ +Settings = require('settings-sharelatex') +redis = require('redis-sharelatex') +rclient = redis.createClient(Settings.redis.web) +logger = require("logger-sharelatex") + +module.exports = UserSessionsManager = + + _sessionSetKey: (user) -> + console.log ">>", user + return "UserSessions:#{user._id}" + + onLogin: (user, sessionId, callback=(err)-> ) -> + logger.log {user_id: user._id, sessionId}, "onLogin handler" + sessionSetKey = UserSessionsManager._sessionSetKey(user) + rclient.sadd sessionSetKey, sessionId, (err, response) -> + if err + logger.err {err, user_id: user._id, sessionId}, "error while adding session key to UserSessions set" + return callback(err) + callback() + + onLogout: (user, sessionId, callback=(err)-> ) -> + logger.log {user_id: user._id, sessionId}, "onLogout handler" + if !user + logger.log {sessionId}, "no user, for some reason" + return callback() + sessionSetKey = UserSessionsManager._sessionSetKey(user) + rclient.srem sessionSetKey, sessionId, (err, response) -> + if err + logger.err {err, user_id: user._id, sessionId}, "error while removing session key from UserSessions set" + return callback(err) + callback()