Skip to content

denwa-nest-shared

Shared NestJS module for DTO validation, RBAC, TypeORM query building, and OpenAPI client

auth-security

core
156 linesSource

Implementing security and RBAC using Roles, Public, and identity decorators. Handles GraphQL and REST contexts uniformly. Includes field-level role checking.

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.

ts
import { Resolver, Mutation, Args } from '@nestjs/graphql';
import { Roles, GetCurrentUserId } from 'denwa-nest-shared';

@Resolver()
export class UserResolver {
  @Roles(['admin', 'manager'])
  @Mutation(() => Boolean)
  async deleteUser(@Args('id') id: string, @GetCurrentUserId() userId: string) {
    // userId is extracted correctly for both GQL and REST
    return true;
  }
}

Core Patterns

Public Routes

Bypass global authentication guards for specific handlers.

ts
import { Public } from 'denwa-nest-shared';

@Public()
@Get('health')
getHealth() {
  return 'ok';
}

Full User Object Extraction

Extract the complete user object from the request context.

ts
import { GetCurrentUser } from 'denwa-nest-shared';

@Get('profile')
getProfile(@GetCurrentUser() user: any) {
  return user;
}

Field-Level Role Protection

Apply restrictions to specific GraphQL fields within a request.

ts
import { Resolver, ResolveField, Parent } from '@nestjs/graphql';
import { CheckFieldsRoles, FieldsRolesInterceptor } from 'denwa-nest-shared';
import { UseInterceptors } from '@nestjs/common';

@Resolver(() => User)
@UseInterceptors(FieldsRolesInterceptor)
export class UserResolver {
  @CheckFieldsRoles({ email: ['admin'] })
  @ResolveField(() => String)
  async email(@Parent() user: User) {
    return user.email;
  }
}

Common Mistakes

HIGH Direct Access to req.user

Wrong:

ts
@Mutation()
async update(@Context() ctx) {
  const user = ctx.req.user; // Potential failure if context not normalized
}

Correct:

ts
@Mutation()
async update(@GetCurrentUser() 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:

ts
@Roles(['admin', 'super-admin'])
@Mutation()
async delete() {}

Correct:

ts
@Roles(['admin'])
@Mutation()
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:

ts
@CheckFieldsRoles({ salary: ['admin'] })
@ResolveField()
async salary() {}

Correct:

ts
@UseInterceptors(FieldsRolesInterceptor)
@CheckFieldsRoles({ salary: ['admin'] })
@ResolveField()
async salary() {}

The @CheckFieldsRoles 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