I am using UI Router in Angular 2 for rout
UIRouterModule.forRoot({
states: APP_STATES,
useHash: true,
config: routerConfigFn,
otherwise: { state: 'home' }
})
The home state requires auth:
export const homeState = {
parent: 'app',
name: 'home',
url: '/home',
component: HomeComponent,
data: {requiresAuth: true}
};
Because it requires auth, I redirect to login page using the example here: https://github.com/ui-router/sample-app-angular/blob/master/src/app/global/auth.hook.ts
export function requiresAuthHook(transitionService: TransitionService) {
const requiresAuthCriteria = {
to: (state) => state.data && state.data.requiresAuth
};
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 });
}
};
transitionService.onBefore(requiresAuthCriteria, redirectToLogin, {priority: 10});
}
But when I launch the app, it enters into an infinite loop. My login state does not require auth, $state.target('login) still runs requiresAuthHook.