Optional authentication in Nest.js with @nestjs/passport

前端 未结 1 624
一向
一向 2020-12-16 20:14

I have a route that needs to be used by authenticated and unauthenticated users. I use @UseGuards(AuthGuard(\'jwt\')) to enable authentication but it prevents a

相关标签:
1条回答
  • 2020-12-16 20:37

    You can just create your own AuthGuard for example by extending the existing one:

    export class OptionalJwtAuthGuard extends AuthGuard('jwt') {
    
      // Override handleRequest so it never throws an error
      handleRequest(err, user, info, context) {
        return user;
      }
    
    }
    

    And then use this one on your controllers instead:

    @UseGuards(OptionalJwtAuthGuard)
    
    0 讨论(0)
提交回复
热议问题