Reputation: 475
I use $stateChangeStart
for check user when change state.
This is my code
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams, options) {
if (!toState.data.require_authentication) {
return;
}
// Because state change is called two times, to prevent infinite loop I remove is_authenticate for next re-check and return
if (toState.data.is_authenticate) {
toState.data.is_authenticate = false;
return;
}
event.preventDefault();
toState.data.is_authenticate = false;
// Check validity of token
authService.checkAuthToken(function(result, status) {
if (result.fn.result.done) {
toState.data.is_authenticate = true;
//$state.go(toState, toParams, options);
$state.go(toState, toParams);
} else {
$log.debug("AuthToken expire/wrong");
storageService.reset();
$state.go("page.login");
}
return;
});
});
With some console log I see that when $state.go is fired, for some reason $stateChangeStart
is fired again, so I need to return out by a boolean params or I enter in an infinite loop...
Why?
Upvotes: 0
Views: 1919
Reputation: 17647
I suggest to check some articles on UI Router and Authentication, however you can substitute $state.go()
with $state.transitionTo()
and setting notify
option to false to avoid broadcast of $stateChangeStart
event.
Interesting articles:
Upvotes: 1