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

Commit

Permalink
Merge pull request #901 from igorauad/fixRedirection
Browse files Browse the repository at this point in the history
Fix redirection to previous state after required authentication
  • Loading branch information
ilanbiala committed Sep 26, 2015
2 parents 48b1a9c + 2b8bee0 commit 5901b17
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 5901b17

Please sign in to comment.