NestJs - Unable to get user context in RolesGuard

前端 未结 2 1071
你的背包
你的背包 2021-02-13 15:32

I\'m using NestJS as the framework for a client API. Within the framework we are using a pretty standard Passport/JWT auth infrastructure that is working fine. Our AuthGuard i

相关标签:
2条回答
  • 2021-02-13 15:51

    Ultimately this appears to be an ordering issue with the guards and it doesn't look like it can be easily resolved (without the framework allowing some control over the ordering).

    My hope was to register the RolesGuard globally but that causes it to be registered first and fire first.

    @UseGuards(AuthGuard('jwt'), RolesGuard)
    @Roles('admin')
    

    If I register it at the endpoint level and put it after the AuthGuard then it fires second and I get the user context I am expecting within the guard itself. It isn't perfect but it works.

    -Kevin

    0 讨论(0)
  • 2021-02-13 16:05

    register RoleGuard at the endpoint level and put it after the AuthGuard then it fires second and I get the user context I am expecting within the guard itself. don't register RoleGuard at module causes it'll be registered first and fire first.

    *.module.ts

    imports: [],
      providers: [{provide: APP_GUARD, useClass: RolesGuard} ,],  // remove guard
      controllers: [],
      exports: [],
    
    0 讨论(0)
提交回复
热议问题