denwa-nest-shared
Version:
Shared NestJS module for DTO validation, RBAC, TypeORM query building, and OpenAPI client
156 lines (114 loc) • 3.58 kB
Markdown
---
name: auth-security
description: >
Implementing security and RBAC using Roles, Public, and identity decorators.
Handles GraphQL and REST contexts uniformly. Includes field-level role checking.
type: core
library: denwa-nest-shared
library_version: '1.0.80'
sources:
- 'Denwa799/nest-shared:src/decorators.ts'
- 'Denwa799/nest-shared:src/interceptors/fields-roles.interceptor.ts'
---
# RBAC & Identity Decorators
Use this skill to protect routes and mutations, and to extract user information regardless of whether the request came from a GraphQL resolver or a standard REST controller.
## Setup
Protect a resolver with roles and extract the current user ID.
```typescript
import { Resolver, Mutation, Args } from '@nestjs/graphql';
import { Roles, GetCurrentUserId } from 'denwa-nest-shared';
()
export class UserResolver {
(['admin', 'manager'])
(() => Boolean)
async deleteUser(('id') id: string, () userId: string) {
// userId is extracted correctly for both GQL and REST
return true;
}
}
```
## Core Patterns
### Public Routes
Bypass global authentication guards for specific handlers.
```typescript
import { Public } from 'denwa-nest-shared';
()
('health')
getHealth() {
return 'ok';
}
```
### Full User Object Extraction
Extract the complete user object from the request context.
```typescript
import { GetCurrentUser } from 'denwa-nest-shared';
('profile')
getProfile(() user: any) {
return user;
}
```
### Field-Level Role Protection
Apply restrictions to specific GraphQL fields within a request.
```typescript
import { Resolver, ResolveField, Parent } from '@nestjs/graphql';
import { CheckFieldsRoles, FieldsRolesInterceptor } from 'denwa-nest-shared';
import { UseInterceptors } from '@nestjs/common';
(() => User)
(FieldsRolesInterceptor)
export class UserResolver {
({ email: ['admin'] })
(() => String)
async email(() user: User) {
return user.email;
}
}
```
## Common Mistakes
### HIGH Direct Access to req.user
Wrong:
```typescript
()
async update(() ctx) {
const user = ctx.req.user; // Potential failure if context not normalized
}
```
Correct:
```typescript
()
async update(() user: User) {
// Safe extraction across GQL and REST
}
```
Directly accessing the request object can fail in mixed environments. The identity decorators handle the underlying context normalization automatically.
Source: Denwa799/nest-shared:src/decorators.ts:121
### MEDIUM Listing super-admin in Roles
Wrong:
```typescript
(['admin', 'super-admin'])
()
async delete() {}
```
Correct:
```typescript
(['admin'])
()
async delete() {}
```
The library's `FieldsRolesInterceptor` and standard role guards typically bypass all restrictions for `super-admin` or `super_admin`. You do not need to specify them explicitly in projects.
Source: maintainer interview
### LOW Forgetting FieldsRolesInterceptor
Wrong:
```typescript
({ salary: ['admin'] })
()
async salary() {}
```
Correct:
```typescript
(FieldsRolesInterceptor)
({ salary: ['admin'] })
()
async salary() {}
```
The `` decorator only stores metadata; it requires the `FieldsRolesInterceptor` to be active on the class or handler to actually enforce the restriction.
Source: Denwa799/nest-shared:src/interceptors/fields-roles.interceptor.ts