What is the purpose of a JWT authentication guard in NestJS?
A JWT authentication guard protects routes by checking if the incoming request has a valid JWT token. It ensures only authenticated users can access certain parts of the app.
Click to reveal answer
intermediate
How does a JWT authentication guard verify a token in NestJS?
It extracts the token from the request header, then uses a JWT service to verify the token's signature and expiration. If valid, it allows access; otherwise, it denies it.
Click to reveal answer
intermediate
Which NestJS interface must a JWT guard implement?
It must implement the CanActivate interface, which requires a canActivate method to decide if the request should proceed.
Click to reveal answer
beginner
What happens if the JWT token is missing or invalid in a NestJS JWT guard?
The guard throws an UnauthorizedException, which stops the request and sends a 401 Unauthorized response to the client.
Click to reveal answer
beginner
How do you apply a JWT authentication guard to a route in NestJS?
You use the @UseGuards() decorator on the controller or route handler and pass the JWT guard class to it.
Click to reveal answer
What does a JWT authentication guard check in a NestJS app?
AIf the database is connected
BIf the user has admin rights
CIf the request is from a mobile device
DIf the request has a valid JWT token
✗ Incorrect
A JWT authentication guard checks the presence and validity of a JWT token in the request.
Which method must a NestJS guard implement?
AcanActivate
BhandleRequest
CvalidateToken
Dauthenticate
✗ Incorrect
The CanActivate interface requires the canActivate method to decide if a request can proceed.
Where is the JWT token usually found in an HTTP request for NestJS guards?
AURL query
BAuthorization header
CRequest body
DCookies
✗ Incorrect
JWT tokens are commonly sent in the Authorization header as a Bearer token.
What response status code does NestJS send if JWT guard denies access?
A401 Unauthorized
B403 Forbidden
C404 Not Found
D500 Internal Server Error
✗ Incorrect
UnauthorizedException triggers a 401 Unauthorized response when access is denied.
How do you protect a NestJS route with a JWT guard?
ASet a route prefix
BAdd a middleware function
CUse @UseGuards(JwtAuthGuard) decorator
DUse @Controller('jwt') decorator
✗ Incorrect
The @UseGuards decorator applies the JWT guard to routes or controllers.
Explain how a JWT authentication guard works in NestJS to protect routes.
Think about the steps from receiving a request to deciding access.
You got /4 concepts.
Describe how to apply a JWT authentication guard to a NestJS controller or route.
Focus on the decorator usage in NestJS.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a JWT authentication guard in a NestJS application?
easy
A. To verify the JWT token and protect routes from unauthorized access
B. To generate new JWT tokens for users
C. To store user data in the database
D. To handle HTTP request logging
Solution
Step 1: Understand the role of JWT guard
The JWT authentication guard checks if the incoming request has a valid JWT token.
Step 2: Identify what the guard protects
The guard protects routes by allowing access only if the token is valid, preventing unauthorized users.
Final Answer:
To verify the JWT token and protect routes from unauthorized access -> Option A
Quick Check:
JWT guard = verify token and protect routes [OK]
Hint: JWT guard checks token validity to protect routes [OK]
Common Mistakes:
Confusing guard with token generation
Thinking guard stores user data
Assuming guard handles logging
2. Which of the following is the correct way to apply a JWT authentication guard to a controller route in NestJS?
easy
A. Use @Get('jwt') decorator on the route
B. @Controller('jwt') before the class
C. Inject JwtService directly in the route handler
D. @UseGuards(AuthGuard('jwt')) before the route handler method
Solution
Step 1: Recall how guards are applied in NestJS
Guards are applied using the @UseGuards() decorator before route handlers or controllers.
Step 2: Identify the correct guard usage for JWT
The JWT guard is used as AuthGuard('jwt') inside @UseGuards decorator.
Final Answer:
@UseGuards(AuthGuard('jwt')) before the route handler method -> Option D
C. The catch block returns true, allowing access on token errors
D. The user is not attached to the request
Solution
Step 1: Review the catch block behavior
The catch block returns true, which means even if token verification fails, access is allowed.
Step 2: Understand correct error handling in guards
On verification failure, the guard should deny access by returning false or throwing an exception.
Final Answer:
The catch block returns true, allowing access on token errors -> Option C
Quick Check:
Catch returning true = always allow access [OK]
Hint: Catch block must deny access, not allow it [OK]
Common Mistakes:
Returning true in catch block
Ignoring token verification errors
Not attaching user to request
5. You want to create a custom JWT authentication guard that extracts the user from the token and adds a custom property isAdmin based on the user's role. Which approach correctly extends the default JWT guard to achieve this?
hard
A. Extend AuthGuard('jwt'), override handleRequest to add isAdmin to user, then return user
B. Create a new guard without extending AuthGuard and manually decode token
C. Use a middleware instead of a guard to add isAdmin property
D. Override canActivate to return true always and add isAdmin in controller
Solution
Step 1: Understand extending AuthGuard('jwt')
Extending the default JWT guard allows reuse of token validation logic.
Step 2: Override handleRequest to customize user object
handleRequest receives the validated user; adding isAdmin here modifies the user before passing it on.
Step 3: Return the modified user to allow access
Returning the user with isAdmin set ensures the controller can access this property safely.
Final Answer:
Extend AuthGuard('jwt'), override handleRequest to add isAdmin to user, then return user -> Option A
Quick Check:
Extend guard + override handleRequest = customize user [OK]
Hint: Extend AuthGuard and override handleRequest to customize user [OK]
Common Mistakes:
Not extending AuthGuard and duplicating logic
Using middleware instead of guard for auth
Overriding canActivate incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.