Complete the code to apply a single guard to a controller method.
import { Controller, Get, [1] } from '@nestjs/common'; @Controller('cats') export class CatsController { @Get() @[1](AuthGuard) findAll() { return 'This action returns all cats'; } }
The @UseGuards decorator is used to apply guards to controller methods or classes in NestJS.
Complete the code to apply multiple guards to a controller method.
import { Controller, Get, UseGuards } from '@nestjs/common'; import { AuthGuard } from './auth.guard'; import { RolesGuard } from './roles.guard'; @Controller('dogs') export class DogsController { @Get() @UseGuards([1]) findAll() { return 'This action returns all dogs'; } }
To apply multiple guards, you pass an array of guard classes to @UseGuards.
Fix the error in the code to correctly combine guards on a controller class.
import { Controller, UseGuards } from '@nestjs/common'; import { AuthGuard } from './auth.guard'; import { RolesGuard } from './roles.guard'; @UseGuards([1]) @Controller('users') export class UsersController { // methods here }
When applying multiple guards at the class level, you must pass them as an array to @UseGuards.
Fill both blanks to create a controller method guarded by AuthGuard and RolesGuard, and import the necessary decorators.
import { Controller, Get, [1] } from '@nestjs/common'; import { AuthGuard } from './auth.guard'; import { RolesGuard } from './roles.guard'; @Controller('products') export class ProductsController { @Get() @[2]([AuthGuard, RolesGuard]) findAll() { return 'This action returns all products'; } }
The UseGuards decorator must be imported and used to apply multiple guards to a method.
Fill all three blanks to define a controller class guarded by AuthGuard and RolesGuard, and import the necessary decorators.
import { [1], [2] } from '@nestjs/common'; import { AuthGuard } from './auth.guard'; import { RolesGuard } from './roles.guard'; @[3]([AuthGuard, RolesGuard]) @Controller('orders') export class OrdersController { // methods here }
You must import UseGuards and Controller decorators, then apply @UseGuards with the guards array on the class.