So, I'm trying to protect the access to several routes by using guards. I'm using the following routes to do so :
const adminRoutes : Routes = [
{
path: 'admin',
component: AdminComponent,
canActivate: [ AuthGuardService ],
children : [
{
path: '',
canActivateChild: [ AuthGuardService ],
children: [
{ path: 'edit', component: DashboardComponent},
{ path: '', component: DashboardComponent}
]
}
]
}
];
Here's a look at what AuthGuardService
looks like
import { Injectable } from '@angular/core';
import {CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot} from "@angular/router";
@Injectable()
export class AuthGuardService implements CanActivate{
constructor(private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
console.log("Guarding...");
return this.sessionValid();
}
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
console.log("Guarding children...");
return this.canActivate(route, state);
}
sessionValid() : boolean {
//tests
}
}
When I try to access to '/admin' and '/admin/edit' with canActivate
only (canActivateChild
is commented) the console displays
Guarding...
When I remove canActivate
and bring canActivateChild
back the console displays
Guarding children...
When I keep both, it goes back to displaying Guarding...
.
So, my question is what's the purpose of having canActivateChild
when canActivate
protects both the root element and the children ?
PS : I get it that canActivateChild
runs before the child route is activated. But what are the benefits of that ? Isn't keeping only one of them sufficient ?