CodeZettaInterview Hub

NestJS · Guards · Junior · Live Coding

How do you build role-based guards with custom decorators?

Short Interview Answer

Set metadata with SetMetadata; Reflector reads roles in the guard; compare to user claims.

Detailed Explanation

Compose @Roles('admin') on handlers. Keep role checks out of controllers. Prefer central policy when complexity grows.

Example

export const Roles = (...roles: string[]) => SetMetadata('roles', roles);

Did you know this answer?