Angular reload url but not view state

I am working on an application using OAuth2 protocol and I am facing a problem with the UI.router module.

In fact, before changing each state, I need to check if the access token is stored in sessionStorage. So I use a function run

like this:

.run(['$state', '$window', '$location', '$rootScope', function ($state, $window, $location, $rootScope) {
            $rootScope.$on('$stateChangeStart',
                function (event, toState, toParams, fromState, fromParams) {
                    var accesstoken = sessionStorage.getItem('access_token');
                    if (!accesstoken) {

                        if ($location.url() !== '/access/connexion') {
                            $state.go('access.connexion', null, { reload: true });
                        }
                    }
                });
        }])

      

We now admit that I am not connected and I am trying to access a URL like / users / 12355 without an access token:

The thing is, the url is updated going from / users / 12355 to / access / connexion, but the view is still the same as the view / users / 12355 (without any information loaded because I don't have an access token)

I do not know what's the problem...

Also, if I'm a console.log

little higher $state.go

, it seems like I'm going through 400 times in this log. If I remove the function $state.go

, I only go through once ...

Can you help me?

NB: access.connexion is the root state

+3


source to share


2 answers


Try to prevent the state from changing if the token is not found:



               if (!accesstoken) {
                     event.preventDefault();

                    if ($location.url() !== '/access/connexion') {
                        $state.go('access.connexion', null, { reload: true });
                    }
                }

      

+1


source


adding notify: true

to the $state.go

json parameters should solve it, change this:

$state.go('access.connexion', null, { reload: true });

      



in

$state.go('access.connexion', null, { reload: true, notify: true });

      

0


source







All Articles