Skip to content

Commit

Permalink
fix(authentication) Stops error on signin/signup (meanjs#1495)
Browse files Browse the repository at this point in the history
Uses the passport info object to simplify login and remove the need to
temporarily cache the redirect within the session.
  • Loading branch information
Wuntenn authored and hyperreality committed Sep 17, 2016
1 parent b0650fe commit b77508f
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand All @@ -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)));
Expand All @@ -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);
};
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
});
Expand Down

0 comments on commit b77508f

Please sign in to comment.