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

Commit e6a35a7

Browse files
committed
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.
1 parent 09870db commit e6a35a7

File tree

5 files changed

+65
-32
lines changed

5 files changed

+65
-32
lines changed

modules/core/client/app/init.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ angular.module(ApplicationConfiguration.applicationModuleName).run(function($roo
3232
}
3333
}
3434
});
35+
// Record previous state
36+
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
37+
$state.previous = {
38+
state: fromState,
39+
params: fromParams,
40+
href: $state.href(fromState, fromParams)
41+
};
42+
});
3543
});
3644

3745
//Then define the init function for starting up the application

modules/users/client/controllers/authentication.client.controller.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
angular.module('users').controller('AuthenticationController', ['$scope', '$http', '$location', 'Authentication',
4-
function($scope, $http, $location, Authentication) {
3+
angular.module('users').controller('AuthenticationController', ['$scope', '$state', '$http', '$location', '$window', 'Authentication',
4+
function($scope, $state, $http, $location, $window, Authentication) {
55
$scope.authentication = Authentication;
66

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

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

30-
// And redirect to the index page
31-
$location.path('/');
30+
// And redirect to the previous or home page
31+
$state.go($state.previous.state.name || 'home', $state.previous.params);
3232
}).error(function(response) {
3333
$scope.error = response.message;
3434
});
3535
};
36+
37+
// OAuth provider request
38+
$scope.callOauthProvider = function(url) {
39+
var redirect_to;
40+
41+
if ($state.previous) {
42+
redirect_to = $state.previous.href;
43+
}
44+
45+
// Effectively call OAuth authentication route:
46+
$window.location.href = url + (redirect_to ? '?redirect_to=' + encodeURIComponent(redirect_to) : '');
47+
};
3648
}
3749
]);
Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
<section class="row">
1+
<section class="row" data-ng-controller="AuthenticationController">
22
<h3 class="col-md-12 text-center">Sign in using your social accounts</h3>
33
<div class="col-md-12 text-center">
4-
<a href="/api/auth/facebook" target="_self" class="undecorated-link">
5-
<img src="/modules/users/img/buttons/facebook.png">
6-
</a>
7-
<a href="/api/auth/twitter" target="_self" class="undecorated-link">
8-
<img src="/modules/users/img/buttons/twitter.png">
9-
</a>
10-
<a href="/api/auth/google" target="_self" class="undecorated-link">
11-
<img src="/modules/users/img/buttons/google.png">
12-
</a>
13-
<a href="/api/auth/linkedin" target="_self" class="undecorated-link">
14-
<img src="/modules/users/img/buttons/linkedin.png">
15-
</a>
16-
<a href="/api/auth/github" target="_self" class="undecorated-link">
17-
<img src="/modules/users/img/buttons/github.png">
18-
</a>
19-
<a href="/api/auth/paypal" target="_self" class="undecorated-link">
20-
<img src="/modules/users/img/buttons/paypal.png">
21-
</a>
4+
<img ng-click="callOauthProvider('/api/auth/facebook')" ng-src="/modules/users/img/buttons/facebook.png">
5+
<img ng-click="callOauthProvider('/api/auth/twitter')" ng-src="/modules/users/img/buttons/twitter.png">
6+
<img ng-click="callOauthProvider('/api/auth/google')" ng-src="/modules/users/img/buttons/google.png">
7+
<img ng-click="callOauthProvider('/api/auth/linkedin')" ng-src="/modules/users/img/buttons/linkedin.png">
8+
<img ng-click="callOauthProvider('/api/auth/github')" ng-src="/modules/users/img/buttons/github.png">
9+
<img ng-click="callOauthProvider('/api/auth/paypal')" ng-src="/modules/users/img/buttons/paypal.png">
2210
</div>
2311
<div ui-view></div>
2412
</section>

modules/users/server/controllers/users/users.authentication.server.controller.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ var path = require('path'),
99
passport = require('passport'),
1010
User = mongoose.model('User');
1111

12+
// URLs for which user can't be redirected on signin
13+
var noReturnUrls = [
14+
'/authentication/signin',
15+
'/authentication/signup'
16+
];
17+
1218
/**
1319
* Signup
1420
*/
@@ -77,11 +83,30 @@ exports.signout = function (req, res) {
7783
res.redirect('/');
7884
};
7985

86+
/**
87+
* OAuth provider call
88+
*/
89+
exports.oauthCall = function(strategy, scope) {
90+
return function(req, res, next) {
91+
// Set redirection path on session.
92+
// Do not redirect to a signin or signup page
93+
if (noReturnUrls.indexOf(req.query.redirect_to) === -1) {
94+
req.session.redirect_to = req.query.redirect_to;
95+
}
96+
// Authenticate
97+
passport.authenticate(strategy, scope)(req, res, next);
98+
};
99+
};
100+
80101
/**
81102
* OAuth callback
82103
*/
83104
exports.oauthCallback = function (strategy) {
84105
return function (req, res, next) {
106+
// Pop redirect URL from session
107+
var sessionRedirectURL = req.session.redirect_to;
108+
delete req.session.redirect_to;
109+
85110
passport.authenticate(strategy, function (err, user, redirectURL) {
86111
if (err) {
87112
return res.redirect('/authentication/signin?err=' + encodeURIComponent(errorHandler.getErrorMessage(err)));
@@ -94,7 +119,7 @@ exports.oauthCallback = function (strategy) {
94119
return res.redirect('/authentication/signin');
95120
}
96121

97-
return res.redirect(redirectURL || '/');
122+
return res.redirect(redirectURL || sessionRedirectURL || '/');
98123
});
99124
})(req, res, next);
100125
};

modules/users/server/routes/auth.server.routes.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ module.exports = function(app) {
2020
app.route('/api/auth/signout').get(users.signout);
2121

2222
// Setting the facebook oauth routes
23-
app.route('/api/auth/facebook').get(passport.authenticate('facebook', {
23+
app.route('/api/auth/facebook').get(users.oauthCall('facebook', {
2424
scope: ['email']
2525
}));
2626
app.route('/api/auth/facebook/callback').get(users.oauthCallback('facebook'));
2727

2828
// Setting the twitter oauth routes
29-
app.route('/api/auth/twitter').get(passport.authenticate('twitter'));
29+
app.route('/api/auth/twitter').get(users.oauthCall('twitter'));
3030
app.route('/api/auth/twitter/callback').get(users.oauthCallback('twitter'));
3131

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

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

5050
// Setting the github oauth routes
51-
app.route('/api/auth/github').get(passport.authenticate('github'));
51+
app.route('/api/auth/github').get(users.oauthCall('github'));
5252
app.route('/api/auth/github/callback').get(users.oauthCallback('github'));
5353

5454
// Setting the paypal oauth routes
55-
app.route('/api/auth/paypal').get(passport.authenticate('paypal'));
55+
app.route('/api/auth/paypal').get(users.oauthCall('paypal'));
5656
app.route('/api/auth/paypal/callback').get(users.oauthCallback('paypal'));
5757
};

0 commit comments

Comments
 (0)