Performance: Guard interface (canActivate)
This affects the request handling speed and responsiveness by controlling route access before controller execution.
async canActivate(context: ExecutionContext): Promise<boolean> { const user = await this.userService.findUserAsync(); return user?.isActive ?? false; }
canActivate(context: ExecutionContext): boolean {
// heavy synchronous logic like complex DB queries or CPU-intensive tasks
const user = this.userService.findUserSync();
return user && user.isActive;
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous heavy logic in canActivate | N/A | N/A | N/A | [X] Bad |
| Asynchronous lightweight checks in canActivate | N/A | N/A | N/A | [OK] Good |
canActivate method in a NestJS guard?canActivatecanActivate method returns true or false to allow or deny access.canActivate in NestJS?class AlwaysDenyGuard implements CanActivate {
canActivate(context: ExecutionContext): boolean {
return false;
}
}class MyGuard implements CanActivate {
canActivate() {
return true;
}
}canActivate implementations correctly checks the role from the request object?context.switchToHttp().getRequest() to get the request object.request.user?.role to avoid errors if user is undefined.Pass the test and this pattern moves unknown