Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Guard interface from NestJS.
NestJS
import { [1] } from '@nestjs/common';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Guard' instead of 'CanActivate'.
Importing 'Controller' or 'Module' by mistake.
✗ Incorrect
The CanActivate interface is used to create guards in NestJS.
2fill in blank
mediumComplete the code to implement the canActivate method in a guard.
NestJS
canActivate(context: ExecutionContext): [1] { return true; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning void or string instead of boolean.
Forgetting to return a value.
✗ Incorrect
The canActivate method must return a boolean to allow or deny access.
3fill in blank
hardFix the error in the guard class declaration.
NestJS
export class AuthGuard implements [1] { }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Guard' or 'Interceptor' instead of 'CanActivate'.
Confusing guards with pipes.
✗ Incorrect
Guards must implement the CanActivate interface to control access.
4fill in blank
hardFill both blanks to create a guard that denies access when user is not authenticated.
NestJS
canActivate(context: ExecutionContext): [1] { const request = context.switchToHttp().getRequest(); return request.user [2] undefined; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning void instead of boolean.
Using '===' instead of '!==' causing wrong access control.
✗ Incorrect
The method returns a boolean. It denies access if request.user is not set.
5fill in blank
hardFill all three blanks to create a guard that allows access only if user role is 'admin'.
NestJS
canActivate(context: ExecutionContext): [1] { const request = context.switchToHttp().getRequest(); const user = request.user; return user?.role [2] [3]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!==' instead of '===' causing wrong access control.
Returning non-boolean values.
✗ Incorrect
The guard returns a boolean. It checks if user.role is exactly 'admin' to allow access.