Bird
0
0

Consider the following NestJS controller method guarded by JwtAuthGuard:

medium📝 Predict Output Q5 of 15
NestJS - Guards
Consider the following NestJS controller method guarded by JwtAuthGuard:

@UseGuards(JwtAuthGuard)
@Get('profile')
getProfile() {
  return { profile: 'user data' };
}

What will happen if a request is made without an Authorization header?
AThe request will throw a server error (500).
BThe request will succeed and return the profile data.
CThe request will be redirected to the login page.
DThe request will be denied with a 401 Unauthorized error.
Step-by-Step Solution
Solution:
  1. Step 1: Understand JwtAuthGuard behavior

    JwtAuthGuard checks for a valid JWT token in the Authorization header.
  2. Step 2: Missing Authorization header

    If the header is missing, the guard will reject the request and respond with 401 Unauthorized.
  3. Final Answer:

    The request will be denied with a 401 Unauthorized error. -> Option D
  4. Quick Check:

    Missing token means no authentication [OK]
Quick Trick: No token means 401 Unauthorized response [OK]
Common Mistakes:
  • Assuming the request passes without a token
  • Expecting a redirect instead of an error
  • Confusing 401 with 500 server error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes