What is the purpose of the canActivate method in a NestJS guard?
The canActivate method decides if a request can proceed to the route handler. It returns true to allow or false to block access.
Click to reveal answer
beginner
How do you implement a guard using the canActivate method in NestJS?
Create a class that implements the <code>CanActivate</code> interface and define the <code>canActivate(context: ExecutionContext)</code> method to return a boolean, Promise<boolean>, or Observable<boolean>.
Click to reveal answer
intermediate
What does the ExecutionContext parameter provide inside canActivate?
It gives access to details about the current request, like the HTTP request object, handler, and class, so you can check user info or request data.
Click to reveal answer
intermediate
Can canActivate return asynchronous results? How?
Yes, it can return a Promise<boolean> or an Observable<boolean>. NestJS will wait for the result before allowing or blocking access.
Click to reveal answer
beginner
Where do you apply a guard that uses canActivate in a NestJS app?
You can apply it at the controller level, route handler level, or globally to protect routes by adding the guard with decorators or in the app module.
Click to reveal answer
What should the canActivate method return to allow access to a route?
Afalse
Bnull
Ctrue
Dundefined
✗ Incorrect
Returning true means the guard allows the request to proceed.
Which interface must a guard class implement to use canActivate?
APipeTransform
BCanDeactivate
COnModuleInit
DCanActivate
✗ Incorrect
The CanActivate interface requires the canActivate method.
What does the ExecutionContext NOT provide inside canActivate?
AAccess to the database connection
BAccess to the route handler
CAccess to the HTTP request
DAccess to the controller class
✗ Incorrect
ExecutionContext does not provide direct access to the database connection.
Can canActivate return an Observable<boolean>?
AYes
BNo
COnly if wrapped in a Promise
DOnly if synchronous
✗ Incorrect
NestJS supports returning an Observable from canActivate.
Where can you apply a guard in a NestJS application?
AOnly on route handlers
BOn controllers, route handlers, or globally
COnly globally
DOnly in middleware
✗ Incorrect
Guards can be applied at multiple levels: controller, route handler, or globally.
Explain how the canActivate method works in a NestJS guard and what it controls.
Think about how guards decide if a user can enter a page.
You got /3 concepts.
Describe how you would create and apply a simple guard using canActivate in a NestJS app.
Imagine you want to block or allow users based on a rule.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the canActivate method in a NestJS guard?
easy
A. To format the response data
B. To handle database connections
C. To decide if a request should be allowed or denied access
D. To log user activity
Solution
Step 1: Understand the role of guards in NestJS
Guards are used to control access to routes by allowing or denying requests.
Step 2: Identify the function of canActivate
The canActivate method returns true or false to allow or deny access.
Final Answer:
To decide if a request should be allowed or denied access -> Option C
Quick Check:
Guard access control = canActivate returns true/false [OK]
Hint: Remember: canActivate returns true to allow access [OK]
Common Mistakes:
Confusing canActivate with response formatting
Thinking canActivate handles database logic
Assuming canActivate logs user actions
2. Which of the following is the correct syntax to implement a guard class with canActivate in NestJS?
easy
A. class MyGuard { canActivate(context) { return true; } }
B. function MyGuard() { canActivate() { return true; } }
5. You want to create a guard that only allows access if the user role is 'admin'. Which of these canActivate implementations correctly checks the role from the request object?