I have a route with two canActivate
guards (AuthGuard
and RoleGuard
). The first (AuthGuard
) checks to see if the user is logged in and, if not, redirects to the login page. The second checks to see if the user has a role defined that is allowed to view the page and, if not, redirects to the un-authorized page.
canActivate: [ AuthGuard, RoleGuard ]
...
export class AuthGuard implements CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
...
this.router.navigate(['/login']);
resolve(false);
}
export class RoleGuard implements CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
...
this.router.navigate(['/unauthorized']);
resolve(false);
}
The problem is that when I access the route and I am not logged in I hit the AuthGuard
, which fails and tells the router to navigate to /login
. However, even though the AuthGuard
failed, the RoleGuard
runs anyway which then navigates to /unauthorized
.
In my opinion it is pointless to run the next guard if the first fails. Is there any way to enforce this behavior?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…