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

Commit

Permalink
Fix redirection to previous state after required authentication
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
igorauad committed Sep 19, 2015
1 parent fba6eb7 commit 2b8bee0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
20 changes: 14 additions & 6 deletions modules/core/client/app/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,30 @@ angular.module(ApplicationConfiguration.applicationModuleName).run(function ($ro
if (Authentication.user !== undefined && typeof Authentication.user === 'object') {
$state.go('forbidden');
} else {
$state.go('authentication.signin');
$state.go('authentication.signin').then(function () {
storePreviousState(toState, toParams);
});
}
}
}
});

// Record previous state
$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
if (!fromState.data || !fromState.data.ignoreState) {
storePreviousState(fromState, fromParams);
});

// Store previous state
function storePreviousState(state, params) {
// only store this state if it shouldn't be ignored
if (!state.data || !state.data.ignoreState) {
$state.previous = {
state: fromState,
params: fromParams,
href: $state.href(fromState, fromParams)
state: state,
params: params,
href: $state.href(state, params)
};
}
});
}
});

//Then define the init function for starting up the application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
scope,
$httpBackend,
$stateParams,
$state,
$location;

beforeEach(function () {
Expand Down Expand Up @@ -59,6 +60,32 @@
expect($location.url()).toEqual('/');
});

it('should be redirected to previous state after successful login',
inject(function (_$state_) {
$state = _$state_;
$state.previous = {
state: {
name: 'articles.create'
},
params: {},
href: '/articles/create'
};

spyOn($state, 'transitionTo');
spyOn($state, 'go');

// Test expected GET request
$httpBackend.when('POST', '/api/auth/signin').respond(200, 'Fred');

scope.signin(true);
$httpBackend.flush();

// Test scope value
expect($state.go).toHaveBeenCalled();
expect($state.go).toHaveBeenCalledWith($state.previous.state.name, $state.previous.params);

}));

it('should fail to log in with nothing', function () {
// Test expected POST request
$httpBackend.expectPOST('/api/auth/signin').respond(400, {
Expand Down

0 comments on commit 2b8bee0

Please sign in to comment.