Bird
0
0

Which of the following is the correct way to apply a JWT authentication guard to a controller route in NestJS?

easy📝 Syntax Q12 of 15
NestJS - Guards
Which 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 route
B@Controller('jwt') before the class
CInject JwtService directly in the route handler
D@UseGuards(AuthGuard('jwt')) before the route handler method
Step-by-Step Solution
Solution:
  1. Step 1: Recall how guards are applied in NestJS

    Guards are applied using the @UseGuards() decorator before route handlers or controllers.
  2. Step 2: Identify the correct guard usage for JWT

    The JWT guard is used as AuthGuard('jwt') inside @UseGuards decorator.
  3. Final Answer:

    @UseGuards(AuthGuard('jwt')) before the route handler method -> Option D
  4. Quick Check:

    @UseGuards(AuthGuard('jwt')) = correct syntax [OK]
Quick Trick: Use @UseGuards(AuthGuard('jwt')) to protect routes [OK]
Common Mistakes:
  • Using @Controller('jwt') instead of guard
  • Injecting JwtService instead of using guard
  • Confusing route decorators with guards

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes