I'm working on an app that has a lot of roles that I need to use guards to block nav to parts of the app based on those roles. I realize I can create individual guard classes for each role, but would rather have one class that I could somehow pass a parameter into. In other words I would like to be able to do something similar to this:
{
path: 'super-user-stuff',
component: SuperUserStuffComponent,
canActivate: [RoleGuard.forRole('superUser')]
}
But since all you pass is the type name of your guard, can't think of a way to do that. Should I just bit the bullet and write the individual guard classes per role and shatter my illusion of elegance in having a single parameterized type instead?
You must do this.
instead using
forRole()
, you should using this:and use this in your RoleGuard
Here's my take on this and a possible solution for the missing provider issue.
In my case, we have a guard that takes a permission or list of permissions as parameter, but it's the same thing has having a role.
We have a class for dealing with auth guards with or without permission:
This deals with checking user active session, etc.
It also contains a method used to obtain a custom permission guard, which is actually depending on the
AuthGuardService
itselfThis allows us to use the method to register some custom guards based on permissions parameter in our routing module:
The interesting part of
forPermission
isAuthGuardService.guards.push
- this basically makes sure that any timeforPermissions
is called to obtain a custom guard class it will also store it in this array. This is also static on the main class:Then we can use this array to register all guards - this is ok as long as we make sure that by the time the app module registers these providers, the routes had been defined and all the guard classes had been created (e.g. check import order and keep these providers as low as possible in the list - having a routing module helps):
Hope this helps.
@AluanHaddad's solution is giving "no provider" error. Here is a fix for that (it feels dirty, but I lack the skills to make a better one).
Conceptually, I register, as a provider, each dynamically generated class created by
roleGuard
.So for every role checked:
you should have:
However, @AluanHaddad's solution as-is will generate new class for each call to
roleGuard
, even ifroles
parameter is the same. Usinglodash.memoize
it looks like this:Note, each combination of roles generates a new class, so you need to register as a provider every combination of roles. I.e. if you have:
canActivate: [roleGuard('foo')]
andcanActivate: [roleGuard('foo', 'bar')]
you will have to register both:providers[roleGuard('foo'), roleGuard('foo', 'bar')]
A better solution would be to register providers automatically in a global providers collection inside
roleGuard
, but as I said, I lack the skills to implement that.