denwa-nest-shared
auth-security
coreImplementing 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.
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.
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.
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.
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:
@Mutation()
async update(@Context() ctx) {
const user = ctx.req.user; // Potential failure if context not normalized
}Correct:
@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:
@Roles(['admin', 'super-admin'])
@Mutation()
async delete() {}Correct:
@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:
@CheckFieldsRoles({ salary: ['admin'] })
@ResolveField()
async salary() {}Correct:
@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