0
0
NestJSframework~10 mins

Reflector and custom decorators in NestJS - Interactive Code Practice

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

Complete the code to create a custom decorator that sets metadata with the key 'role'.

NestJS
import { SetMetadata } from '@nestjs/common';

export const Roles = (role: string) => SetMetadata('[1]', role);
Drag options to blanks, or click blank then click option'
Arole
Baccess
Cpermission
Droles
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular 'role' instead of 'roles' causes metadata retrieval issues.
Using unrelated keys like 'permission' or 'access' won't match expected metadata.
2fill in blank
medium

Complete the code to inject Reflector in a guard constructor.

NestJS
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';

@Injectable()
export class RolesGuard implements CanActivate {
  constructor(private readonly [1]: Reflector) {}

  canActivate(context: ExecutionContext): boolean {
    // guard logic
    return true;
  }
}
Drag options to blanks, or click blank then click option'
AReflector
Breflector
Creflect
DreflectorService
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'Reflector' as variable name causes errors.
Using unrelated names like 'reflect' or 'reflectorService' breaks convention.
3fill in blank
hard

Fix the error in retrieving metadata inside the guard using Reflector.

NestJS
const roles = this.reflector.get<string[]>('[1]', context.getHandler());
Drag options to blanks, or click blank then click option'
Aroles
Brole
Cpermission
Daccess
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular 'role' causes the metadata to be undefined.
Using unrelated keys like 'permission' or 'access' returns no metadata.
4fill in blank
hard

Fill both blanks to create a custom decorator and retrieve metadata in a guard.

NestJS
export const Roles = (role: string) => SetMetadata([1], role);

const roles = this.reflector.get<string[]>([2], context.getHandler());
Drag options to blanks, or click blank then click option'
A'roles'
B'role'
C'permissions'
D'access'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys in decorator and reflector causes metadata mismatch.
Forgetting quotes around the key causes syntax errors.
5fill in blank
hard

Fill all three blanks to create a guard that checks roles metadata and returns access decision.

NestJS
canActivate(context: ExecutionContext): boolean {
  const roles = this.reflector.get<string[]>([1], context.getHandler());
  if (!roles) {
    return [2];
  }
  const request = context.switchToHttp().getRequest();
  const user = request.user;
  return roles.some(role => user.roles?.includes([3]));
}
Drag options to blanks, or click blank then click option'
A'roles'
Btrue
Crole
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false when no roles metadata causes unwanted blocking.
Using wrong variable names inside the some() callback causes errors.
Using wrong metadata key causes roles to be undefined.