Bird
0
0

Given this code snippet, what will happen if a request has an invalid JWT token? ```typescript @UseGuards(JwtAuthGuard) @Get('profile') getProfile() { return { message: 'Access granted' }; } ```

medium📝 Predict Output Q4 of 15
NestJS - Guards
Given this code snippet, what will happen if a request has an invalid JWT token? ```typescript @UseGuards(JwtAuthGuard) @Get('profile') getProfile() { return { message: 'Access granted' }; } ```
AThe server crashes due to unhandled exception
BThe request proceeds and returns 'Access granted'
CThe request is denied with 401 Unauthorized error
DThe request is redirected to login page
Step-by-Step Solution
Solution:
  1. Step 1: Understand JwtAuthGuard behavior on invalid token

    JwtAuthGuard denies access and throws 401 Unauthorized if token is invalid.
  2. Step 2: Analyze the route behavior with guard

    The route handler runs only if guard passes; invalid token blocks access.
  3. Final Answer:

    The request is denied with 401 Unauthorized error -> Option C
  4. Quick Check:

    Invalid token = 401 error [OK]
Quick Trick: Invalid JWT causes 401 Unauthorized by default [OK]
Common Mistakes:
  • Assuming invalid token lets request pass
  • Expecting server crash on invalid token
  • Thinking guard redirects automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes