Skip to content

Commit

Permalink
Merge pull request #602 from OpenUserJs/login-redirect
Browse files Browse the repository at this point in the history
Redirect the user to their previous page after login/logout.
  • Loading branch information
sizzlemctwizzle committed Mar 18, 2015
2 parents 244e55c + ac71cf9 commit d94380e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
12 changes: 6 additions & 6 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down
27 changes: 23 additions & 4 deletions controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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);
});
});
};

0 comments on commit d94380e

Please sign in to comment.