Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the necessary decorator for roles.
NestJS
import { [1] } from '@nestjs/common';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Roles decorator which does not exist by default.
Confusing UseGuards with metadata setting.
✗ Incorrect
The SetMetadata decorator is used to define custom metadata like roles in NestJS.
2fill in blank
mediumComplete the code to create a custom decorator named Roles.
NestJS
export const Roles = (...roles: string[]) => [1]('roles', roles);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using UseGuards instead of SetMetadata.
Trying to use Injectable which is for services.
✗ Incorrect
SetMetadata is used to create custom decorators that attach metadata like roles.
3fill in blank
hardFix the error in the guard method to get roles metadata.
NestJS
const roles = this.reflector.get<string[]>([1], context.getHandler()); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'role' instead of 'roles' causes undefined metadata.
Using unrelated keys like 'permissions' or 'auth'.
✗ Incorrect
The metadata key used for roles is 'roles', so it must match exactly.
4fill in blank
hardFill in the blank to check if user roles include any required role.
NestJS
if (!roles) return true; // Allow access if no roles are set return user.roles.some(role => roles.[1](role)); // Check if user has any required role
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'excludes' which is not a valid array method.
Returning false when no roles are set blocks access incorrectly.
✗ Incorrect
The includes method checks if the user's role is in the required roles array. Returning true if no roles are set allows open access.
5fill in blank
hardFill all three blanks to apply the Roles guard to a controller method.
NestJS
@[1](RolesGuard) @[2]('admin') @[3]('dashboard') async getDashboard() { return 'Admin Dashboard'; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Controller instead of Get for the route method.
Forgetting to apply UseGuards to activate the guard.
✗ Incorrect
UseGuards applies the guard, Roles sets the required roles, and Get defines the route method.