nestjs-prisma-starter icon indicating copy to clipboard operation
nestjs-prisma-starter copied to clipboard

Add role guard and decorator to support RBAC

Open zlwu opened this issue 3 years ago • 0 comments

Add RolesGuard guard and and Roles decorator to support Role-Based Access Control.

This is an example for users with role USER or ADMIN can call updateUser().


@Resolver(() => User)
@UseGuards(GqlAuthGuard)
export class UsersResolver {

  // ...


  @Roles(Role.USER, Role.ADMIN)
  @UseGuards(GqlAuthGuard, RolesGuard)
  @Mutation(() => User)
  async updateUser(
    @UserEntity() user: User,
    @Args('data') newUserData: UpdateUserInput
  ) {
    return this.usersService.updateUser(user.id, newUserData);
  }

  // ...
}

zlwu avatar Aug 24 '22 15:08 zlwu