Skip to content

Instantly share code, notes, and snippets.

@jsanta
Created May 29, 2018 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsanta/08e26726365890b936df7ef9ce29c491 to your computer and use it in GitHub Desktop.
Save jsanta/08e26726365890b936df7ef9ce29c491 to your computer and use it in GitHub Desktop.
Auth Hook with the routeEnabled condition included (for Angular UI Router Sample App)
const redirectToLogin = (transition) => {
const authService: AuthService = transition.injector().get(AuthService);
const $state = transition.router.stateService;
if (!authService.isAuthenticated()) {
return $state.target('login', undefined, { location: false });
} else {
const toState = transition.to();
try {
if (!toState.data.routeEnabled) {
return $state.target(null, undefined, { location: false });
}
} catch (routeException) {
console.error('Could not validate routeEnabled', routeException);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment