Created
May 29, 2018 20:35
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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