Complete the code to import the guard decorator in NestJS.
import { [1] } from '@nestjs/common';
The UseGuards decorator is used to apply guards to routes in NestJS.
Complete the code to apply a guard to a route handler.
@UseGuards([1]) @Get('profile') getProfile() { return 'User profile'; }
The AuthGuard is a common guard used to protect routes by checking authentication.
Fix the error in the guard class by completing the method signature.
canActivate([1]): boolean { // logic here return true; }
The canActivate method receives an ExecutionContext object in NestJS guards.
Fill both blanks to create a guard that checks if a user is authenticated.
import { Injectable, CanActivate, [1] } from '@nestjs/common'; @Injectable() export class AuthGuard implements CanActivate { canActivate([2]): boolean { const request = context.switchToHttp().getRequest(); return !!request.user; } }
The guard imports ExecutionContext and the canActivate method receives context: ExecutionContext as parameter.
Fill all three blanks to apply the AuthGuard to a controller route.
import { Controller, Get, [1] } from '@nestjs/common'; @Controller('users') export class UserController { @[2]([3]) @Get('dashboard') getDashboard() { return 'Dashboard data'; } }
The UseGuards decorator is imported and used to apply the AuthGuard to the route.
