mirror of
https://github.com/taobataoma/meanTorrent.git
synced 2026-02-04 21:49:18 +01:00
Enable redirection to previous page after login
Two different strategies are adopted, one for when the user authenticates locally and the other through providers. When authenticating locally, the signin function in the client controller redirects to the previous state (storing and using a state name) after successful login. When authenticating through a provider, the first call to provider stores the previous URL (not state, URL) in the session. Then, when provider actually calls the authentication callback, session redirect_to path is used for redirecting user.
This commit is contained in:
@@ -9,6 +9,12 @@ var path = require('path'),
|
||||
passport = require('passport'),
|
||||
User = mongoose.model('User');
|
||||
|
||||
// URLs for which user can't be redirected on signin
|
||||
var noReturnUrls = [
|
||||
'/authentication/signin',
|
||||
'/authentication/signup'
|
||||
];
|
||||
|
||||
/**
|
||||
* Signup
|
||||
*/
|
||||
@@ -77,11 +83,30 @@ exports.signout = function (req, res) {
|
||||
res.redirect('/');
|
||||
};
|
||||
|
||||
/**
|
||||
* OAuth provider call
|
||||
*/
|
||||
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);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* OAuth callback
|
||||
*/
|
||||
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;
|
||||
|
||||
passport.authenticate(strategy, function (err, user, redirectURL) {
|
||||
if (err) {
|
||||
return res.redirect('/authentication/signin?err=' + encodeURIComponent(errorHandler.getErrorMessage(err)));
|
||||
@@ -94,7 +119,7 @@ exports.oauthCallback = function (strategy) {
|
||||
return res.redirect('/authentication/signin');
|
||||
}
|
||||
|
||||
return res.redirect(redirectURL || '/');
|
||||
return res.redirect(redirectURL || sessionRedirectURL || '/');
|
||||
});
|
||||
})(req, res, next);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user