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:
Igor Freire
2015-03-22 11:47:14 -03:00
parent 09870db9ce
commit e6a35a7f9a
5 changed files with 65 additions and 32 deletions

View File

@@ -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:
@@ -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;
});
@@ -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) : '');
};
}
]);