Complete the code to implement the canActivate method in a NestJS guard.
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
return [1];
}The canActivate method must return true to allow access.
Complete the code to extract the request object inside canActivate using ExecutionContext.
const request = context.switchToHttp().[1]();Use getRequest() to access the HTTP request object inside the guard.
Fix the error in the guard method to correctly check if user is authenticated.
const user = request.user; if (!user) { throw new [1]('Unauthorized'); } return true;
Throw UnauthorizedException when the user is not authenticated.
Fill both blanks to create a guard that allows access only if user role is 'admin'.
const user = request.user; return user?.role [1] [2];
The guard returns true only if user.role === 'admin'.
Fill all three blanks to implement a guard that checks if request method is POST and user is authenticated.
const request = context.switchToHttp().getRequest(); if (request.method [1] 'POST' && request.user [2] null) { return [3]; } return false;
The guard checks if method is 'POST' and user is not null, then returns true to allow access.