fix(users): fix redirect when signup or add provider (#1573)

Fixes the issue of the redirect after Social login authentication, on signup or user profile add provider.
This commit is contained in:
itelo
2016-10-19 22:12:47 -02:00
committed by Michael Leanos
parent ae638893b2
commit 0e2ea65918
3 changed files with 29 additions and 18 deletions

View File

@@ -85,6 +85,9 @@ exports.signout = function (req, res) {
*/
exports.oauthCall = function (strategy, scope) {
return function (req, res, next) {
if (req.query && req.query.redirect_to)
req.session.redirect_to = req.query.redirect_to;
// Authenticate
passport.authenticate(strategy, scope)(req, res, next);
};
@@ -119,6 +122,14 @@ exports.oauthCallback = function (strategy) {
* Helper function to save or update a OAuth user profile
*/
exports.saveOAuthUserProfile = function (req, providerUserProfile, done) {
// Setup info object
var info = {};
// Set redirection path on session.
// Do not redirect to a signin or signup page
if (noReturnUrls.indexOf(req.session.redirect_to) === -1)
info.redirect_to = req.session.redirect_to;
if (!req.user) {
// Define a search query fields
var searchMainProviderIdentifierField = 'providerData.' + providerUserProfile.providerIdentifierField;
@@ -138,15 +149,6 @@ 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);
@@ -198,7 +200,7 @@ exports.saveOAuthUserProfile = function (req, providerUserProfile, done) {
// And save the user
user.save(function (err) {
return done(err, user, '/settings/accounts');
return done(err, user, info);
});
} else {
return done(new Error('User is already connected using this provider'), user);