Is there a way to set a "base" canActivate
when configuring routes in Angular2? So that all routes are covered by the same base check, and then each route can have a more granular check.
I have an AppModule
with routing like this:
@NgModule({
imports: [
RouterModule.forRoot([
{
path: '',
component: HomeComponent,
canActivate: [AuthenticationGuardService],
data: { roles: [Roles.User] }
}
])
],
exports: [
RouterModule
]
})
export class AppRoutingModule { }
And for the feature module FeatureModule
:
@NgModule({
imports: [
RouterModule.forChild([
{
path: "feature",
component: FeatureComponent,
// I'd like to avoid having to do this:
canActivate: [AuthenticationGuardService],
data: { roles: [Roles.User] }
}
])
],
exports: [
RouterModule
]
})
export class FeatureRoutingModule { }
I let AuthenticationGuardService
check if a user has access to a route by using the roles provided in data
.
Can I do something to avoid having to set canActivate
and data
in all my feature routing modules? I'd like to just configure a "base" canActivate
for all routes in this application.
RouterModule.forChild([helperService.getRoute("feature", FeatureComponent)])
? And then that helper service (or class or method..) always adds the base Guard to the generated route object? – BucaramangaRouterModule.forChild([new AuthenticatedRoute({ path: ..., component: ... })])
– Julesjuleydata
though – Bibliopole