Recall & Review
beginner
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?✗ Incorrect
Returning
true means the guard allows the request to proceed.Which interface must a guard class implement to use
canActivate?✗ Incorrect
The
CanActivate interface requires the canActivate method.What does the
ExecutionContext NOT provide inside canActivate?✗ Incorrect
ExecutionContext does not provide direct access to the database connection.Can
canActivate return an Observable<boolean>?✗ Incorrect
NestJS supports returning an Observable from
canActivate.Where can you apply a guard in a NestJS application?
✗ 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.