nest-router icon indicating copy to clipboard operation
nest-router copied to clipboard

support canActivate Guards on routes

Open xmlking opened this issue 6 years ago • 9 comments

It is kind of hard to defined Guards (e.g., AuthGuard) at global level but exclude at specific route with standard @nestjs. it would be nice if this package provide a way to define canActivate with route definition so that we can have some control over customizing enforcing guards declaratively.

xmlking avatar Jul 13 '18 03:07 xmlking

Hi @xmlking , it seems a good idea, but is there any API Propasol so we can have a clear idea ?

shekohex avatar Jul 13 '18 20:07 shekohex

Thinking something like this.

const routes: Routes = [
    {
      path: '/api',
      canActivate: [AuthGuard] ,
      children: [
        {
          path: '/cats',
          module: CatsModule,
          canActivate: [CatGuard] 
        },
        {
          path: '/dogs',
          module: DogsModule,
          canActivate: [DogGuard] 
        },
      ],
    },
  ];

The question for discussion: if there are more then one effective Guards on a route, how we can compose and apply guards to that route? should we apply and condition or should we react based on previous guard's result!

xmlking avatar Jul 15 '18 00:07 xmlking

Hmm, i'm thinking since Guards are injectable, and we can add it directly to providers: [] Nest Router actually have access to that providers array, we can just push it there with APP_GUARD token.

In my opinion it just a helper or a method sugar for applying guards to modules.

shekohex avatar Jul 15 '18 00:07 shekohex

Agree, most of us adding guards via APP_GUARD and activating via decorators. My concern was in general how nestjs handle multiple global guards as I am asking for clarification here https://github.com/nestjs/nest/issues/873

xmlking avatar Jul 15 '18 15:07 xmlking

APP_GUARD is a global guard. We should introduce a sort of MODULE_GUARD in this case.

kamilmysliwiec avatar Mar 08 '19 08:03 kamilmysliwiec

Hi @xmlking , it seems a good idea, but is there any API Propasol so we can have a clear idea ?

I think this can be done similar like its done in angular.

gkucmierz avatar May 13 '19 12:05 gkucmierz

APP_GUARD is a global guard. We should introduce a sort of MODULE_GUARD in this case.

Agree

gkucmierz avatar May 13 '19 12:05 gkucmierz

If so (no MODULE_GUARD), is there currently no way to set guard on a per module basis in NestJS?

CatsMiaow avatar Sep 11 '19 17:09 CatsMiaow

Is there any updates on this issue?

cerireyhan avatar May 18 '20 19:05 cerireyhan