Recall & Review
beginner
What is the purpose of a guard in NestJS?
A guard in NestJS controls access to routes by deciding if a request should proceed based on conditions like authentication or roles.
Click to reveal answer
beginner
How do you apply a guard to protect a route in NestJS?
You apply a guard by using the @UseGuards() decorator on a controller or route handler to check conditions before allowing access.
Click to reveal answer
intermediate
What method must a NestJS guard implement?
A NestJS guard must implement the canActivate() method, which returns true or false (or a Promise/Observable of these) to allow or deny access.
Click to reveal answer
intermediate
How can you access the current request inside a guard?
Inside canActivate(), you can access the request object via the ExecutionContext parameter using context.switchToHttp().getRequest().
Click to reveal answer
beginner
What happens if a guard returns false in canActivate()?
If canActivate() returns false, NestJS denies the request and responds with a 403 Forbidden error by default.
Click to reveal answer
Which decorator is used to apply a guard to a route in NestJS?
✗ Incorrect
The @UseGuards() decorator is the correct way to apply guards to routes or controllers.
What does the canActivate() method in a guard return to allow access?
✗ Incorrect
Returning true from canActivate() means the request is allowed to proceed.
Where do you get the HTTP request object inside a guard?
✗ Incorrect
The correct way is context.switchToHttp().getRequest() to access the request in HTTP context.
What HTTP status code is returned if a guard denies access?
✗ Incorrect
By default, NestJS returns 403 Forbidden when a guard returns false.
Can guards in NestJS be asynchronous?
✗ Incorrect
Guards can be async by returning a Promise or Observable from canActivate().
Explain how to create and use a guard to protect a route in NestJS.
Think about how NestJS checks access before running route handlers.
You got /5 concepts.
Describe what happens when a guard denies access to a route in NestJS.
Focus on the flow after canActivate returns false.
You got /4 concepts.