Skip to content

Snapshot for @system-core/core 0.10.x. Current docs live at /.

NestJS

Import from @system-core/core/integrations/nestjs.

Use this when NestJS owns the HTTP shell and system-core owns the auth or CMS runtime behind it.

Install

bash
npm install @system-core/core @nestjs/common
npm install zod bcryptjs jsonwebtoken nodemailer

Add @prisma/client only when you are also using the package-owned Prisma schema.

Basic Setup

Register SystemCoreModule with your HTTP adapter and either your own AuthDeps or a runtime created elsewhere.

ts
import { Module } from '@nestjs/common'
import { SystemCoreModule, nestjsAdapter } from '@system-core/core/integrations/nestjs'
import type { AuthDeps } from '@system-core/core'

const authDeps: AuthDeps = {
  users: { /* ... */ },
  roles: { /* ... */ },
  permissions: { /* ... */ },
  rolePermissions: { /* ... */ },
  userPermissions: { /* ... */ },
  invites: { /* ... */ },
  passwordResets: { /* ... */ },
  emailVerifications: { /* ... */ },
  sessions: { /* ... */ }
}

@Module({
  imports: [
    SystemCoreModule.forRoot({
      http: nestjsAdapter,
      auth: authDeps
    })
  ]
})
export class AppModule {}

SystemCoreModule.forRoot() now returns a real NestJS DynamicModule, so it can be imported directly in AppModule without the old startup crash.

Guards

Use guards with a package-owned auth runtime, not the raw adapter bag. For custom schemas, build that runtime from your AuthDeps.

ts
import { Controller, Get, Post, UseGuards } from '@nestjs/common'
import { createAuthSystem, createConfig } from '@system-core/core'
import { createAuthGuard, createPermissionGuard } from '@system-core/core/integrations/nestjs'

const authSystem = createAuthSystem(authDeps, createConfig())

const AuthGuard = createAuthGuard(authSystem)
const CanPublishGuard = createPermissionGuard(
  'cms:publish',
  (userId, permission) => authSystem.authorization.hasEffectivePermission(userId, permission)
)

@Controller('content')
export class ContentController {
  @Get('me')
  @UseGuards(AuthGuard)
  me() {
    return { ok: true }
  }

  @Post('publish')
  @UseGuards(AuthGuard, CanPublishGuard)
  publish() {
    return { published: true }
  }
}

createAuthGuard(authSystem) verifies the bearer access token through auth.sessions.verifyToken() and stores the resolved session payload on request.user.

DI Injection

Everything you pass into forRoot() is registered under SYSTEM_CORE_TOKEN.

ts
import { Controller, Get, Inject, Injectable } from '@nestjs/common'
import type { AuthDeps } from '@system-core/core'
import { SYSTEM_CORE_TOKEN } from '@system-core/core/integrations/nestjs'
import type { SystemCoreProviders } from '@system-core/core/integrations/nestjs'

@Injectable()
export class AuthAdapterService {
  constructor(
    @Inject(SYSTEM_CORE_TOKEN) private readonly core: SystemCoreProviders
  ) {}

  get authDeps(): AuthDeps {
    return this.core.auth as AuthDeps
  }
}

@Controller('health')
export class HealthController {
  constructor(private readonly authAdapter: AuthAdapterService) {}

  @Get()
  check() {
    return { authConfigured: !!this.authAdapter.authDeps.sessions }
  }
}

Custom Prisma Schemas

If your Prisma schema does not expose the package-owned model names, do not call createPrismaDeps() against it. Build AuthDeps manually and pass it through SystemCoreModule.forRoot({ auth }) or createSystem({ auth }) instead.

Direct Adapter Usage

ts
import { Controller, Get } from '@nestjs/common'
import { getNestjsEvent, nestjsAdapter } from '@system-core/core/integrations/nestjs'

@Controller('echo')
export class EchoController {
  @Get()
  echo(ctx: any) {
    const event = getNestjsEvent(ctx)
    const method = nestjsAdapter.getMethod(event)
    const url = nestjsAdapter.getRequestURL(event)
    return { method, pathname: url.pathname }
  }
}

API Reference

ExportKindDescription
nestjsAdapter / httpAdapterHttpAdapterNestJS request/response adapter
getNestjsEvent(ctx)functionConverts a Nest ExecutionContext into the adapter event shape
createAuthGuard(authSystem)functionFactory returning a bearer-token guard
createPermissionGuard(permission, checkFn)functionFactory returning a permission guard
SystemCoreModuleclassDecorated NestJS module class with .forRoot(config)
SYSTEM_CORE_TOKENconstDI token for whatever you registered via forRoot()
SystemCoreProviderstypeProvider bag registered by the module

system-core documentation for maintainers, integrators, and AI build agents.