diff --git a/controllers/auth.js b/controllers/auth.js index 9eb040bc8..3bf855221 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -49,16 +49,16 @@ exports.auth = function (aReq, aRes, aNext) { function auth() { var authenticate = null; - if (strategy === 'google') { - authOpts.scope = ['https://www.googleapis.com/auth/userinfo.profile']; - } - authenticate = passport.authenticate(strategy, authOpts); - // Just in case some dumbass tries a bad /auth/* url if (!strategyInstances[strategy]) { return aNext(); } + if (strategy === 'google') { + authOpts.scope = ['https://www.googleapis.com/auth/userinfo.profile']; + } + authenticate = passport.authenticate(strategy, authOpts); + authenticate(aReq, aRes, aNext); } @@ -180,7 +180,7 @@ exports.callback = function (aReq, aRes, aNext) { } else { // Delete the username that was temporarily stored delete aReq.session.username; - doneUrl = aReq.session.redirectTo || doneUrl; + doneUrl = aReq.session.redirectTo; delete aReq.session.redirectTo; return aRes.redirect(doneUrl); } diff --git a/controllers/index.js b/controllers/index.js index d26a02501..5a6390d47 100644 --- a/controllers/index.js +++ b/controllers/index.js @@ -8,6 +8,7 @@ var isDbg = require('../libs/debug').isDbg; // var async = require('async'); var _ = require('underscore'); +var url = require('url'); var Discussion = require('../models/discussion').Discussion; var Group = require('../models/group').Group; @@ -151,13 +152,30 @@ exports.home = function (aReq, aRes) { async.parallel(tasks, asyncComplete); }; +// Get the referer url for redirect after login/logout +function getRedirect(aReq) { + var referer = aReq.get('Referer'); + var redirect = '/'; + + if (referer) { + referer = url.parse(referer); + if (referer.hostname === aReq.hostname) { + redirect = referer.path; + } + } + + return redirect; +} + // UI for user registration exports.register = function (aReq, aRes) { var authedUser = aReq.session.user; // If already logged in, goto the front page. - if (authedUser) - return aRes.redirect('/'); + if (authedUser) { + return aRes.redirect(getRedirect(aReq)); + } + aReq.session.redirectTo = getRedirect(aReq); // var options = {}; @@ -225,12 +243,13 @@ exports.register = function (aReq, aRes) { exports.logout = function (aReq, aRes) { var authedUser = aReq.session.user; + var redirectUrl = getRedirect(aReq); - if (!authedUser) { return aRes.redirect('/'); } + if (!authedUser) { return aRes.redirect(redirectUrl); } User.findOne({ _id: authedUser._id }, function (aErr, aUser) { removeSession(aReq, aUser, function () { - aRes.redirect('/'); + aRes.redirect(redirectUrl); }); }); };