How do I redirect to a child route from a guard of the parent route while also protecting child routes from direct access*?
Problem with absolute navigation the guard on the parent component is called again in a loop when navigating to the child route.
Problem with relative navigation is that the guard is protecting the parent route so there is no Activated Route yet to navigate relative to. Also this would probably not protect child routes. Maybe the same guard could also be used with the child routes or with CanActivateChildren.
Stackblitz Example: https://stackblitz.com/edit/angular-qbe2a7
routes
const appRoutes: Routes = [
{
path: 'foo',
component: FooComponent,
canActivate: [ RedirectionGuard ],
children: [
{
path: 'foo-child',
component: FooChildComponent
}
]
}
];
canActivate() in guard
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean>|Promise<boolean>|boolean {
console.log('guard: ', route, state, this.route);
// idea: create own ActivatedRoute and give it the values from ActivatedRouteSnapshot which knows about 'foo/'
const foo: ActivatedRoute = new ActivatedRoute();
console.log(foo);
// Error: Cannot match any routes
// I think because there is no route to be relative to when canActivat is called. Router has not yet navigated to '/foo/'
this.router.navigate(['../foo-child'], { relativeTo: this.route
});
// Errors with infinite loop because '/foo/' has this guard which will then be called again
//this.router.navigate(['/foo/foo-child']);
// navigate returns true also. maybe just return that.
return true;
}
*Why not add guards to all childs you may ask. I need to redirect from the parent route to many different child routes depending on the state of the app. I am currently using one of the child routes to redirect from that component but that does not protect child routes form direct navigation by the user.