0
0
NestJSframework~5 mins

JWT authentication guard in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
Which method must a NestJS guard implement?
AcanActivate
BhandleRequest
CvalidateToken
Dauthenticate
Where is the JWT token usually found in an HTTP request for NestJS guards?
AURL query
BAuthorization header
CRequest body
DCookies
What response status code does NestJS send if JWT guard denies access?
A401 Unauthorized
B403 Forbidden
C404 Not Found
D500 Internal Server Error
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
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.