Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 run Guard after another guard resolved

In my project I have two guards. AuthGuard and PermissionGuard. I need to first AuthGuard runs and when it resolved and if true the permissionGuard begins but now this guards are running parallel and permissionGuard not working well. the way I used for this issue is that I called the AuthGuard CanActivate method in Permission guard but I think there is a quite better way for doing this.

like image 805
Hossein Ahmadi Avatar asked Dec 23 '16 07:12

Hossein Ahmadi


People also ask

Can we use multiple guards in Angular?

We can use multiple route guards and then the route will only be accessible when all route guards return true. That's it!

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

CanDeactivate. A third type of guard we can add to our application is a CanDeactivate guard which is usually used to warn people if they are navigating away from a page where they have some unsaved changes.

What is CanActivate and CanDeactivate?

CanActivatelinkInterface that a class can implement to be a guard deciding if a route can be activated. If all guards return true , navigation continues. If any guard returns false , navigation is cancelled.

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

That is called AuthGuard. AuthGuard is used to protect the routes from unauthorized access.


1 Answers

The best way I've seen it done is to expose the router guards on child routes. Here is a working example.

{
  path:'', canActivate:[AuthorizationService1], 
    children: [
      {
        path:'', canActivate:[AuthorizationService2],component: HomeComponent
      }
    ]
}
like image 178
zmanc Avatar answered Oct 21 '22 03:10

zmanc