Guards

There is no difference between microservices guards and the regular guards. The only thing you should be aware of is that instead of throwing HttpException, you should use the RpcException.

info Hint The RpcException class is exposed from @nestjs/microservices package.

Binding guards

Here is an example that makes use of a method-scoped guard (class-scoped works too):

  1. @@filename()
  2. @UseGuards(AuthGuard)
  3. @MessagePattern({ cmd: 'sum' })
  4. accumulate(data: number[]): number {
  5. return (data || []).reduce((a, b) => a + b);
  6. }
  7. @@switch
  8. @UseGuards(AuthGuard)
  9. @MessagePattern({ cmd: 'sum' })
  10. accumulate(data) {
  11. return (data || []).reduce((a, b) => a + b);
  12. }