0
0
NestJSframework~10 mins

Guard binding levels 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 guard at the controller level.

NestJS
import { Controller, UseGuards } from '@nestjs/common';
import { AuthGuard } from './auth.guard';

@UseGuards([1])
@Controller('users')
export class UsersController {
  // controller methods
}
Drag options to blanks, or click blank then click option'
AAuthGuard
BAuthGuard()
CUseGuards
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses after the guard class name in @UseGuards.
Using the wrong decorator name.
2fill in blank
medium

Complete the code to apply a guard at the route handler level.

NestJS
import { Controller, Get, UseGuards } from '@nestjs/common';
import { RolesGuard } from './roles.guard';

@Controller('products')
export class ProductsController {
  @Get()
  @UseGuards([1])
  findAll() {
    return [];
  }
}
Drag options to blanks, or click blank then click option'
ARolesGuard()
BUseGuards
CRolesGuard
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the guard class as a function with parentheses.
Using the wrong decorator.
3fill in blank
hard

Fix the error in applying a guard globally in main.ts.

NestJS
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { LoggingGuard } from './logging.guard';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalGuards([1]);
  await app.listen(3000);
}
bootstrap();
Drag options to blanks, or click blank then click option'
ALoggingGuard()
BLoggingGuard
CUseGuards
Dnew LoggingGuard()
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the guard class instead of an instance.
Calling the guard as a function without new.
4fill in blank
hard

Fill both blanks to apply multiple guards at the controller level.

NestJS
import { Controller, UseGuards } from '@nestjs/common';
import { AuthGuard } from './auth.guard';
import { RolesGuard } from './roles.guard';

@UseGuards([1], [2])
@Controller('orders')
export class OrdersController {
  // methods
}
Drag options to blanks, or click blank then click option'
AAuthGuard
BRolesGuard
CUseGuards
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses after guard class names.
Using decorators other than @UseGuards.
5fill in blank
hard

Fill all three blanks to apply a guard globally and override it at controller and route levels.

NestJS
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { GlobalGuard } from './global.guard';
import { ControllerGuard } from './controller.guard';
import { RouteGuard } from './route.guard';
import { Controller, UseGuards, Get } from '@nestjs/common';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalGuards([1]);
  await app.listen(3000);
}

@UseGuards([2])
@Controller('items')
class ItemsController {
  @Get()
  @UseGuards([3])
  findAll() {
    return [];
  }
}

bootstrap();
Drag options to blanks, or click blank then click option'
Anew GlobalGuard()
BControllerGuard
CRouteGuard
DUseGuards
Attempts:
3 left
💡 Hint
Common Mistakes
Passing class names instead of instances for global guards.
Adding parentheses after controller or route guard class names.