Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Enable redirection to previous page after login
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
igorauad committed Jul 29, 2015
1 parent 09870db commit e6a35a7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 32 deletions.
8 changes: 8 additions & 0 deletions modules/core/client/app/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ angular.module(ApplicationConfiguration.applicationModuleName).run(function($roo
}
}
});
// Record previous state
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
$state.previous = {
state: fromState,
params: fromParams,
href: $state.href(fromState, fromParams)
};
});
});

//Then define the init function for starting up the application
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

angular.module('users').controller('AuthenticationController', ['$scope', '$http', '$location', 'Authentication',
function($scope, $http, $location, Authentication) {
angular.module('users').controller('AuthenticationController', ['$scope', '$state', '$http', '$location', '$window', 'Authentication',
function($scope, $state, $http, $location, $window, Authentication) {
$scope.authentication = Authentication;

// Get an eventual error defined in the URL query string:
Expand All @@ -15,8 +15,8 @@ angular.module('users').controller('AuthenticationController', ['$scope', '$http
// If successful we assign the response to the global user model
$scope.authentication.user = response;

// And redirect to the index page
$location.path('/');
// And redirect to the previous or home page
$state.go($state.previous.state.name || 'home', $state.previous.params);
}).error(function(response) {
$scope.error = response.message;
});
Expand All @@ -27,11 +27,23 @@ angular.module('users').controller('AuthenticationController', ['$scope', '$http
// If successful we assign the response to the global user model
$scope.authentication.user = response;

// And redirect to the index page
$location.path('/');
// And redirect to the previous or home page
$state.go($state.previous.state.name || 'home', $state.previous.params);
}).error(function(response) {
$scope.error = response.message;
});
};

// OAuth provider request
$scope.callOauthProvider = function(url) {
var redirect_to;

if ($state.previous) {
redirect_to = $state.previous.href;
}

// Effectively call OAuth authentication route:
$window.location.href = url + (redirect_to ? '?redirect_to=' + encodeURIComponent(redirect_to) : '');
};
}
]);
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
<section class="row">
<section class="row" data-ng-controller="AuthenticationController">
<h3 class="col-md-12 text-center">Sign in using your social accounts</h3>
<div class="col-md-12 text-center">
<a href="/api/auth/facebook" target="_self" class="undecorated-link">
<img src="/modules/users/img/buttons/facebook.png">
</a>
<a href="/api/auth/twitter" target="_self" class="undecorated-link">
<img src="/modules/users/img/buttons/twitter.png">
</a>
<a href="/api/auth/google" target="_self" class="undecorated-link">
<img src="/modules/users/img/buttons/google.png">
</a>
<a href="/api/auth/linkedin" target="_self" class="undecorated-link">
<img src="/modules/users/img/buttons/linkedin.png">
</a>
<a href="/api/auth/github" target="_self" class="undecorated-link">
<img src="/modules/users/img/buttons/github.png">
</a>
<a href="/api/auth/paypal" target="_self" class="undecorated-link">
<img src="/modules/users/img/buttons/paypal.png">
</a>
<img ng-click="callOauthProvider('/api/auth/facebook')" ng-src="/modules/users/img/buttons/facebook.png">
<img ng-click="callOauthProvider('/api/auth/twitter')" ng-src="/modules/users/img/buttons/twitter.png">
<img ng-click="callOauthProvider('/api/auth/google')" ng-src="/modules/users/img/buttons/google.png">
<img ng-click="callOauthProvider('/api/auth/linkedin')" ng-src="/modules/users/img/buttons/linkedin.png">
<img ng-click="callOauthProvider('/api/auth/github')" ng-src="/modules/users/img/buttons/github.png">
<img ng-click="callOauthProvider('/api/auth/paypal')" ng-src="/modules/users/img/buttons/paypal.png">
</div>
<div ui-view></div>
</section>
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)));
Expand All @@ -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);
};
Expand Down
12 changes: 6 additions & 6 deletions modules/users/server/routes/auth.server.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ module.exports = function(app) {
app.route('/api/auth/signout').get(users.signout);

// Setting the facebook oauth routes
app.route('/api/auth/facebook').get(passport.authenticate('facebook', {
app.route('/api/auth/facebook').get(users.oauthCall('facebook', {
scope: ['email']
}));
app.route('/api/auth/facebook/callback').get(users.oauthCallback('facebook'));

// Setting the twitter oauth routes
app.route('/api/auth/twitter').get(passport.authenticate('twitter'));
app.route('/api/auth/twitter').get(users.oauthCall('twitter'));
app.route('/api/auth/twitter/callback').get(users.oauthCallback('twitter'));

// Setting the google oauth routes
app.route('/api/auth/google').get(passport.authenticate('google', {
app.route('/api/auth/google').get(users.oauthCall('google', {
scope: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email'
Expand All @@ -39,7 +39,7 @@ module.exports = function(app) {
app.route('/api/auth/google/callback').get(users.oauthCallback('google'));

// Setting the linkedin oauth routes
app.route('/api/auth/linkedin').get(passport.authenticate('linkedin', {
app.route('/api/auth/linkedin').get(users.oauthCall('linkedin', {
scope: [
'r_basicprofile',
'r_emailaddress'
Expand All @@ -48,10 +48,10 @@ module.exports = function(app) {
app.route('/api/auth/linkedin/callback').get(users.oauthCallback('linkedin'));

// Setting the github oauth routes
app.route('/api/auth/github').get(passport.authenticate('github'));
app.route('/api/auth/github').get(users.oauthCall('github'));
app.route('/api/auth/github/callback').get(users.oauthCallback('github'));

// Setting the paypal oauth routes
app.route('/api/auth/paypal').get(passport.authenticate('paypal'));
app.route('/api/auth/paypal').get(users.oauthCall('paypal'));
app.route('/api/auth/paypal/callback').get(users.oauthCallback('paypal'));
};

0 comments on commit e6a35a7

Please sign in to comment.