Bird
0
0

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

easy📝 Syntax Q12 of 15
NestJS - Authentication
Which of the following is the correct way to apply an authentication guard in a NestJS controller?
A@AuthGuard('jwt') export class MyController {}
B@UseGuards(AuthGuard('jwt')) export class MyController {}
C@UseAuthGuard('jwt') export class MyController {}
D@GuardUse(Auth('jwt')) export class MyController {}
Step-by-Step Solution
Solution:
  1. Step 1: Recall NestJS guard syntax

    Guards are applied using the @UseGuards() decorator with the guard class or strategy inside.
  2. Step 2: Match the correct syntax for JWT guard

    The correct syntax is @UseGuards(AuthGuard('jwt')) to apply the JWT strategy.
  3. Final Answer:

    @UseGuards(AuthGuard('jwt')) export class MyController {} -> Option B
  4. Quick Check:

    @UseGuards(AuthGuard('jwt')) = Correct syntax [OK]
Quick Trick: Use @UseGuards with AuthGuard('jwt') for authentication [OK]
Common Mistakes:
  • Using wrong decorator names like @AuthGuard
  • Swapping order of decorator and parameters
  • Misspelling @UseGuards

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes