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

Commit 2b8bee0

Browse files
committed
Fix redirection to previous state after required authentication
Fixes the issue with the previous state not being recorded, when the unauthenticated user is redirected to the signin state, when trying to access a restricted route. Added a function that stores the provided state & state params, in the $state.previous object. This has been implemented in the $stateChangeSuccess event, and the callback of the $state.go transition when the user is not allowed to access the requested route.
1 parent fba6eb7 commit 2b8bee0

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

modules/core/client/app/init.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,30 @@ angular.module(ApplicationConfiguration.applicationModuleName).run(function ($ro
3030
if (Authentication.user !== undefined && typeof Authentication.user === 'object') {
3131
$state.go('forbidden');
3232
} else {
33-
$state.go('authentication.signin');
33+
$state.go('authentication.signin').then(function () {
34+
storePreviousState(toState, toParams);
35+
});
3436
}
3537
}
3638
}
3739
});
3840

3941
// Record previous state
4042
$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
41-
if (!fromState.data || !fromState.data.ignoreState) {
43+
storePreviousState(fromState, fromParams);
44+
});
45+
46+
// Store previous state
47+
function storePreviousState(state, params) {
48+
// only store this state if it shouldn't be ignored
49+
if (!state.data || !state.data.ignoreState) {
4250
$state.previous = {
43-
state: fromState,
44-
params: fromParams,
45-
href: $state.href(fromState, fromParams)
51+
state: state,
52+
params: params,
53+
href: $state.href(state, params)
4654
};
4755
}
48-
});
56+
}
4957
});
5058

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

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
scope,
99
$httpBackend,
1010
$stateParams,
11+
$state,
1112
$location;
1213

1314
beforeEach(function () {
@@ -59,6 +60,32 @@
5960
expect($location.url()).toEqual('/');
6061
});
6162

63+
it('should be redirected to previous state after successful login',
64+
inject(function (_$state_) {
65+
$state = _$state_;
66+
$state.previous = {
67+
state: {
68+
name: 'articles.create'
69+
},
70+
params: {},
71+
href: '/articles/create'
72+
};
73+
74+
spyOn($state, 'transitionTo');
75+
spyOn($state, 'go');
76+
77+
// Test expected GET request
78+
$httpBackend.when('POST', '/api/auth/signin').respond(200, 'Fred');
79+
80+
scope.signin(true);
81+
$httpBackend.flush();
82+
83+
// Test scope value
84+
expect($state.go).toHaveBeenCalled();
85+
expect($state.go).toHaveBeenCalledWith($state.previous.state.name, $state.previous.params);
86+
87+
}));
88+
6289
it('should fail to log in with nothing', function () {
6390
// Test expected POST request
6491
$httpBackend.expectPOST('/api/auth/signin').respond(400, {

0 commit comments

Comments
 (0)