Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply router guard to all children except one

Currently, in Angular, you can restrict access to all child routes by applying a router guard to one of the parents:

export const routes: Routes = [
  {
    path: 'my-account',
    canActivate: [IsUserLoggedIn],
    children: [{
      path: 'settings',
      component: SettingsComponent
    }, {
      path: 'edit-profile',
      component; EditProfileComponent
    }]
  }
];

This is useful to avoid having to repeat the canActivate guard in each route. But now what happens when I want to introduce a third route under my-account that should be publicly accessible? For example, maybe there's a publicly-accessible Help page at my-account/help that everyone should be able to access, even when they aren't logged in:

}, {
  path: 'help',
  component: HelpComponent,
  // Somehow make exception to canActivate guard above
}, {

Is there a clean way to do this, or is the only way to disrupt the organization of routes and apply the router guard to each child route manually, except the Help page?

like image 246
A. Duff Avatar asked Feb 09 '18 22:02

A. Duff


People also ask

Which guard will be checking child route access?

The router checks the CanDeactivate and CanActivateChild guards first, from deepest child route to the top. Then it checks the CanActivate guards from the top down to the deepest child route.

Which route Guard is helpful in preventing unauthorized access to a component?

canActivate: It checks if the user can visit the specific route or we have to prevent access to the specific route. For this, we use the CanActivate interface. Here, the canActivate method has two arguments.

Which route Guard would you use to prevent a user from navigating to another view before saving their content?

Now here if we want to prevent navigation of an unauthorized user we can use CanActivate Guard, that will do the job but also download the module. Now to control the navigation as well as prevent downloading of that module we can use CanLoad Guard.

What is the use of router guard?

The Angular router's navigation guards allow to grant or remove access to certain parts of the navigation. Another route guard, the CanDeactivate guard, even allows you to prevent a user from accidentally leaving a component with unsaved changes.


1 Answers

I personally think if the HelpComponent route doesn't need to be protected, it will need to be in different parent route. So, it's the matter of organizing the routes.

But, in any case, you could also create a separate route for it, such as:

export const routes: Routes = [
  {
    path: 'my-account',
    canActivate: [IsUserLoggedIn],
    children: [{
      path: 'settings',
      component: SettingsComponent
    }, {
      path: 'edit-profile',
      component: EditProfileComponent
    }]
  },
  {
    path: 'my-account/help',
    component: HelpComponent
  }
];
like image 197
stack247 Avatar answered Oct 20 '22 00:10

stack247