Angular2 CanActivate guard for all but one route

I know that we can group routes located in one module. For instance:

canActivate: [AuthGuard], children: [ { path: '', children: [ { path: 'crises', component: ManageCrisesComponent }, { path: 'heroes', component: ManageHeroesComponent }, { path: '', component: AdminDashboardComponent } ], } 

But I have to add this protector to every module routing file. And I have a lot of them.

I want the user to be unable to go to any route except one (login) if he is not authorized.

What is the correct way to add protection for all of them?

+7
source share
1 answer

You can use the parent route without blank spaces with protection

 { path: '', canActivate: [AuthGuard], children: [ { path: '', children: [ { path: 'crises', component: ManageCrisesComponent }, { path: 'heroes', component: ManageHeroesComponent }, { path: '', component: AdminDashboardComponent } ], } } 

and in a check if the user is logged in. If you are not logged in and the current route is login , then enable it.

+6
source

Source: https://habr.com/ru/post/1260538/


All Articles