Optional authentication in Nest.js with @nestjs/passport

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)

Leave a Comment

tech