Created
May 29, 2018 19:48
-
-
Save jsanta/4bb9fa3bc594d2c532741e8e6d1c4f6a to your computer and use it in GitHub Desktop.
Copy & pasted from https://github.com/ui-router/sample-app-angular/blob/master/src/app/global/auth.hook.ts
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
import { TransitionService } from '@uirouter/core'; | |
import { AuthService } from './auth.service'; | |
/** | |
* This file contains a Transition Hook which protects a | |
* route that requires authentication. | |
* | |
* This hook redirects to /login when both: | |
* - The user is not authenticated | |
* - The user is navigating to a state that requires authentication | |
*/ | |
export function requiresAuthHook(transitionService: TransitionService) { | |
// Matches if the destination state's data property has a truthy 'requiresAuth' property | |
const requiresAuthCriteria = { | |
to: (state) => state.data && state.data.requiresAuth | |
}; | |
// Function that returns a redirect for the current transition to the login state | |
// if the user is not currently authenticated (according to the AuthService) | |
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 }); | |
} | |
}; | |
// Register the "requires auth" hook with the TransitionsService | |
transitionService.onBefore(requiresAuthCriteria, redirectToLogin, {priority: 10}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment