So, I'm building an Angular 4 app that I want all routes to be protected (except for the login route of course). I'm trying to use feature modules and feature module routing. So, imagine something like this:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AuthGuardService } from './auth/auth-guard.service';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { ProtectedRouteComponent } from './protected-route/protected-route.component';
const routes: Routes = [
{ path: 'login', component: LoginComponent }, // no auth guard for login
{
path: '',
canActivate: [AuthGuardService],
children: [
{
path: '',
children: [
{ path: 'register', component: RegisterComponent },
{ path: 'protected', component: ProtectedRouteComponent }
]
}
]
}
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
, for the root module, and then a users feature module routing config like:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { UsersComponent } from './users.component';
const routes: Routes = [
{ path: 'users', component: UsersComponent }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class UsersRoutingModule {}
What would be the best way to:
- Keep using feature module routing, and
- Apply the route guards to the users feature module routing without replicating the canActivate guard? Is there any way I can pass it on to the users feature module routing from the root routing module?
Thanks,
Iraklis