From 67d1a5a1f690f131d077169daf646b30ae1c3879 Mon Sep 17 00:00:00 2001 From: Daron Jones Date: Sun, 11 Sep 2016 20:29:05 +0100 Subject: [PATCH] fix(authentication) Stops error on signin/signup (#1495) Uses the passport info object to simplify login and remove the need to temporarily cache the redirect within the session. --- .../users.authentication.server.controller.js | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/modules/users/server/controllers/users/users.authentication.server.controller.js b/modules/users/server/controllers/users/users.authentication.server.controller.js index 56a962e8e9..d822d692e5 100644 --- a/modules/users/server/controllers/users/users.authentication.server.controller.js +++ b/modules/users/server/controllers/users/users.authentication.server.controller.js @@ -85,11 +85,6 @@ exports.signout = function (req, res) { */ exports.oauthCall = function (strategy, scope) { return function (req, res, next) { - // Set redirection path on session. - // Do not redirect to a signin or signup page - if (noReturnUrls.indexOf(req.query.redirect_to) === -1) { - req.session.redirect_to = req.query.redirect_to; - } // Authenticate passport.authenticate(strategy, scope)(req, res, next); }; @@ -100,10 +95,8 @@ exports.oauthCall = function (strategy, scope) { */ exports.oauthCallback = function (strategy) { return function (req, res, next) { - // Pop redirect URL from session - var sessionRedirectURL = req.session.redirect_to; - delete req.session.redirect_to; + // info.redirect_to contains inteded redirect path passport.authenticate(strategy, function (err, user, info) { if (err) { return res.redirect('/authentication/signin?err=' + encodeURIComponent(errorHandler.getErrorMessage(err))); @@ -116,7 +109,7 @@ exports.oauthCallback = function (strategy) { return res.redirect('/authentication/signin'); } - return res.redirect(info || sessionRedirectURL || '/'); + return res.redirect(info.redirect_to || '/'); }); })(req, res, next); }; @@ -145,6 +138,15 @@ exports.saveOAuthUserProfile = function (req, providerUserProfile, done) { $or: [mainProviderSearchQuery, additionalProviderSearchQuery] }; + // Setup info object + var info = {}; + + // Set redirection path on session. + // Do not redirect to a signin or signup page + if (noReturnUrls.indexOf(req.query.redirect_to) === -1) { + info.redirect_to = req.query.redirect_to; + } + User.findOne(searchQuery, function (err, user) { if (err) { return done(err); @@ -166,11 +168,11 @@ exports.saveOAuthUserProfile = function (req, providerUserProfile, done) { // And save the user user.save(function (err) { - return done(err, user); + return done(err, user, info); }); }); } else { - return done(err, user); + return done(err, user, info); } } });