NestJS · Middle · Question 4 of 75
How does a roles guard read roles declared on a route?
export const Roles = Reflector.createDecorator<string[]>();
@Roles(["admin"])
@Delete(":id")
remove() {}- ✓Correct answer: It injects
Reflectorand reads the metadata for the handler and class, e.g.reflector.getAllAndOverride(Roles, [context.getHandler(), context.getClass()]) - 2It parses the source code of the controller
- 3Roles are passed in the request body
Explanation
getAllAndOverride lets a method-level value override a class-level one; getAllAndMerge combines them.