Refactor the way logins are finished off and sessions established

This commit is contained in:
Shane Kilkelly 2018-07-17 16:27:24 +01:00
parent b4f8108277
commit 299de369e5

View file

@ -62,16 +62,23 @@ module.exports = AuthenticationController =
if err? if err?
return next(err) return next(err)
if user # `user` is either a user object or false if user # `user` is either a user object or false
redir = AuthenticationController._getRedirectFromSession(req) || "/project" AuthenticationController.finishLogin(user, req, res, next)
AuthenticationController.afterLoginSessionSetup req, user, (err) ->
if err?
return next(err)
AuthenticationController._clearRedirectFromSession(req)
res.json {redir: redir}
else else
res.json message: info res.json message: info
)(req, res, next) )(req, res, next)
finishLogin: (user, req, res, next) ->
redir = AuthenticationController._getRedirectFromSession(req) || "/project"
AuthenticationController._loginAsyncHandlers(req, user)
AuthenticationController.afterLoginSessionSetup req, user, (err) ->
if err?
return next(err)
AuthenticationController._clearRedirectFromSession(req)
if req.headers?['accept']?.match(/^application\/json.*$/)
res.json {redir: redir}
else
res.redirect(redir)
doPassportLogin: (req, username, password, done) -> doPassportLogin: (req, username, password, done) ->
email = username.toLowerCase() email = username.toLowerCase()
LoginRateLimiter.processLoginRequest email, (err, isAllowed)-> LoginRateLimiter.processLoginRequest email, (err, isAllowed)->
@ -83,20 +90,19 @@ module.exports = AuthenticationController =
return done(error) if error? return done(error) if error?
if user? if user?
# async actions # async actions
AuthenticationController._loginAsyncHandlers(req, email, user)
return done(null, user) return done(null, user)
else else
AuthenticationController._recordFailedLogin() AuthenticationController._recordFailedLogin()
logger.log email: email, "failed log in" logger.log email: email, "failed log in"
return done(null, false, {text: req.i18n.translate("email_or_password_wrong_try_again"), type: 'error'}) return done(null, false, {text: req.i18n.translate("email_or_password_wrong_try_again"), type: 'error'})
_loginAsyncHandlers: (req, email, user) -> _loginAsyncHandlers: (req, user) ->
UserHandler.setupLoginData(user, ()->) UserHandler.setupLoginData(user, ()->)
LoginRateLimiter.recordSuccessfulLogin(email) LoginRateLimiter.recordSuccessfulLogin(user.email)
AuthenticationController._recordSuccessfulLogin(user._id) AuthenticationController._recordSuccessfulLogin(user._id)
Analytics.recordEvent(user._id, "user-logged-in", {ip:req.ip}) Analytics.recordEvent(user._id, "user-logged-in", {ip:req.ip})
Analytics.identifyUser(user._id, req.sessionID) Analytics.identifyUser(user._id, req.sessionID)
logger.log email: email, user_id: user._id.toString(), "successful log in" logger.log email: user.email, user_id: user._id.toString(), "successful log in"
req.session.justLoggedIn = true req.session.justLoggedIn = true
# capture the request ip for use when creating the session # capture the request ip for use when creating the session
user._login_req_ip = req.ip user._login_req_ip = req.ip