NestJs - Unable to get user context in RolesGuard

前端 未结 2 1079
你的背包
你的背包 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

提交回复
热议问题