NestJS - AuthenticationWhich 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 {}Check Answer
Step-by-Step SolutionSolution:Step 1: Recall NestJS guard syntaxGuards are applied using the @UseGuards() decorator with the guard class or strategy inside.Step 2: Match the correct syntax for JWT guardThe correct syntax is @UseGuards(AuthGuard('jwt')) to apply the JWT strategy.Final Answer:@UseGuards(AuthGuard('jwt')) export class MyController {} -> Option BQuick Check:@UseGuards(AuthGuard('jwt')) = Correct syntax [OK]Quick Trick: Use @UseGuards with AuthGuard('jwt') for authentication [OK]Common Mistakes:Using wrong decorator names like @AuthGuardSwapping order of decorator and parametersMisspelling @UseGuards
Master "Authentication" in NestJS9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More NestJS Quizzes Authentication - Refresh token pattern - Quiz 8hard Authentication - Token generation and validation - Quiz 8hard Authentication - Session-based authentication - Quiz 14medium Authentication - Refresh token pattern - Quiz 11easy Database with TypeORM - Why TypeORM integrates seamlessly with NestJS - Quiz 1easy Database with TypeORM - Query builder - Quiz 4medium Guards - Guard interface (canActivate) - Quiz 2easy Interceptors - Logging interceptor - Quiz 5medium Middleware - Global middleware - Quiz 14medium Pipes - Pipe binding (parameter, method, controller, global) - Quiz 15hard