0
0
NestJSframework~5 mins

Guard interface (canActivate) in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Afalse
Bnull
Ctrue
Dundefined
Which interface must a guard class implement to use canActivate?
APipeTransform
BCanDeactivate
COnModuleInit
DCanActivate
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
Can canActivate return an Observable<boolean>?
AYes
BNo
COnly if wrapped in a Promise
DOnly if synchronous
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
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.