Bird
0
0

Given this NestJS controller snippet, what happens when a request without a valid JWT token is sent?

medium📝 component behavior Q13 of 15
NestJS - Authentication
Given this NestJS controller snippet, what happens when a request without a valid JWT token is sent?
@UseGuards(AuthGuard('jwt'))
@Get('profile')
getProfile() {
  return { message: 'Access granted' };
}
AThe request is denied with a 401 Unauthorized error.
BThe request returns an empty response.
CThe request causes a server crash.
DThe request is allowed and returns { message: 'Access granted' }.
Step-by-Step Solution
Solution:
  1. Step 1: Understand AuthGuard('jwt') behavior

    This guard checks for a valid JWT token and blocks requests without it.
  2. Step 2: Predict response for missing or invalid token

    Without a valid token, the guard denies access and returns a 401 Unauthorized error.
  3. Final Answer:

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

    Invalid token = 401 error [OK]
Quick Trick: No valid JWT means 401 Unauthorized response [OK]
Common Mistakes:
  • Assuming request passes without token
  • Expecting server crash on missing token
  • Thinking empty response is returned

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes