0
0
NestJSframework~10 mins

Why guards control access in NestJS - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
ACanActivate
BController
CGuard
DModule
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Guard' instead of 'CanActivate'.
Importing 'Controller' or 'Module' by mistake.
2fill in blank
medium

Complete 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'
Astring
Bvoid
Cnumber
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Returning void or string instead of boolean.
Forgetting to return a value.
3fill in blank
hard

Fix the error in the guard class declaration.

NestJS
export class AuthGuard implements [1] { }
Drag options to blanks, or click blank then click option'
AGuard
BInterceptor
CCanActivate
DPipeTransform
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Guard' or 'Interceptor' instead of 'CanActivate'.
Confusing guards with pipes.
4fill in blank
hard

Fill 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'
Aboolean
B!==
C===
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Returning void instead of boolean.
Using '===' instead of '!==' causing wrong access control.
5fill in blank
hard

Fill 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'
Aboolean
B===
C'admin'
D!==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!==' instead of '===' causing wrong access control.
Returning non-boolean values.