NestJS - GuardsWhich of the following is the correct way to apply a JWT authentication guard to a controller route in NestJS?AUse @Get('jwt') decorator on the routeB@Controller('jwt') before the classCInject JwtService directly in the route handlerD@UseGuards(AuthGuard('jwt')) before the route handler methodCheck Answer
Step-by-Step SolutionSolution:Step 1: Recall how guards are applied in NestJSGuards are applied using the @UseGuards() decorator before route handlers or controllers.Step 2: Identify the correct guard usage for JWTThe JWT guard is used as AuthGuard('jwt') inside @UseGuards decorator.Final Answer:@UseGuards(AuthGuard('jwt')) before the route handler method -> Option DQuick Check:@UseGuards(AuthGuard('jwt')) = correct syntax [OK]Quick Trick: Use @UseGuards(AuthGuard('jwt')) to protect routes [OK]Common Mistakes:Using @Controller('jwt') instead of guardInjecting JwtService instead of using guardConfusing route decorators with guards
Master "Guards" in NestJS9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More NestJS Quizzes Authentication - Session-based authentication - Quiz 2easy Database with Prisma - Schema definition - Quiz 15hard Database with TypeORM - Migrations - Quiz 1easy Guards - Reflector and custom decorators - Quiz 10hard Guards - Guard interface (canActivate) - Quiz 9hard Interceptors - Response transformation - Quiz 5medium Interceptors - Cache interceptor - Quiz 1easy Interceptors - Interceptor interface - Quiz 5medium Middleware - Global middleware - Quiz 11easy Pipes - Pipe binding (parameter, method, controller, global) - Quiz 7medium