0
0
NestJSframework~10 mins

Combining multiple guards in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to apply a single guard to a controller method.

NestJS
import { Controller, Get, [1] } from '@nestjs/common';

@Controller('cats')
export class CatsController {
  @Get()
  @[1](AuthGuard)
  findAll() {
    return 'This action returns all cats';
  }
}
Drag options to blanks, or click blank then click option'
AUseGuards
BInject
CSetMetadata
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Inject instead of @UseGuards
Trying to apply guards without a decorator
Confusing @Controller with guard decorators
2fill in blank
medium

Complete the code to apply multiple guards to a controller method.

NestJS
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';
  }
}
Drag options to blanks, or click blank then click option'
AAuthGuard && RolesGuard
BAuthGuard, RolesGuard
C{AuthGuard, RolesGuard}
D[AuthGuard, RolesGuard]
Attempts:
3 left
💡 Hint
Common Mistakes
Passing guards separated by commas without brackets
Using object literal syntax instead of array
Using logical AND operator between guards
3fill in blank
hard

Fix the error in the code to correctly combine guards on a controller class.

NestJS
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
}
Drag options to blanks, or click blank then click option'
AAuthGuard && RolesGuard
B[AuthGuard, RolesGuard]
C{AuthGuard, RolesGuard}
DAuthGuard, RolesGuard
Attempts:
3 left
💡 Hint
Common Mistakes
Passing guards without brackets
Using object literal syntax
Using logical operators instead of array
4fill in blank
hard

Fill both blanks to create a controller method guarded by AuthGuard and RolesGuard, and import the necessary decorators.

NestJS
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';
  }
}
Drag options to blanks, or click blank then click option'
AUseGuards
BSetMetadata
CInject
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong decorator
Using different decorators for import and method
Not passing guards as an array
5fill in blank
hard

Fill all three blanks to define a controller class guarded by AuthGuard and RolesGuard, and import the necessary decorators.

NestJS
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
}
Drag options to blanks, or click blank then click option'
AUseGuards
BGet
CPost
DInject
EController
Attempts:
3 left
💡 Hint
Common Mistakes
Importing wrong decorators
Not applying @UseGuards on the class
Mixing method decorators with class decorators