Skip to content
Discussion options

You must be logged in to vote

Hi @EliasGit117! I'm Dosu and I’m helping the orpc team.

The standard and recommended way to handle role-based route protection in NestJS is to use custom decorators (like @Roles('admin')) together with guards (such as a RolesGuard). The decorator sets metadata on the route, and the guard reads this metadata to enforce access control. This approach is idiomatic, integrates well with NestJS features, and keeps your authorization logic clean and reusable.

Example:

// roles.decorator.ts
import { SetMetadata } from '@nestjs/common';
export const Roles = (...roles: string[]) => SetMetadata('roles', roles);

// roles.guard.ts
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/c…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by unnoq
Comment options

You must be logged in to vote
1 reply
@dosubot
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant