From 2b8bee0c8c64309f29babde696b1c163242654f6 Mon Sep 17 00:00:00 2001 From: Igor Freire Date: Wed, 9 Sep 2015 21:43:55 -0300 Subject: [PATCH] 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. --- modules/core/client/app/init.js | 20 +++++++++----- .../authentication.client.controller.tests.js | 27 +++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/modules/core/client/app/init.js b/modules/core/client/app/init.js index 7a2336ea26..e2200743b5 100644 --- a/modules/core/client/app/init.js +++ b/modules/core/client/app/init.js @@ -30,7 +30,9 @@ 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); + }); } } } @@ -38,14 +40,20 @@ angular.module(ApplicationConfiguration.applicationModuleName).run(function ($ro // 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 diff --git a/modules/users/tests/client/authentication.client.controller.tests.js b/modules/users/tests/client/authentication.client.controller.tests.js index 62022529c7..7242973f74 100644 --- a/modules/users/tests/client/authentication.client.controller.tests.js +++ b/modules/users/tests/client/authentication.client.controller.tests.js @@ -8,6 +8,7 @@ scope, $httpBackend, $stateParams, + $state, $location; beforeEach(function () { @@ -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, {