npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@authcore/nestjs

v0.12.0

Published

NestJS adapter for AuthCore. Dynamic module, guards, and decorators.

Readme

@authcore/nestjs

NestJS adapter for AuthCore. Dynamic module, guards, and decorators.

Install

# Core packages
npm install @authcore/nestjs @authcore/prisma-adapter

# Prisma client (required)
npm install @prisma/client

# Prisma CLI (required for migrations/codegen — dev dependency)
npm install --save-dev prisma

# Prisma 6 only: driver adapter for your database (PostgreSQL example)
npm install @prisma/adapter-pg pg
npm install --save-dev @types/pg

If you are on Prisma 5 you do not need @prisma/adapter-pg. See the @authcore/prisma-adapter README for schema setup and Prisma version details.

Peer dependencies (already installed in any NestJS project):

  • @nestjs/common (^10.0.0 or ^11.0.0)
  • @nestjs/core (^10.0.0 or ^11.0.0)
  • reflect-metadata (^0.1.13 or ^0.2.0)

Usage

Module Setup

// app.module.ts
import { Module } from '@nestjs/common'
import { AuthModule } from '@authcore/nestjs'
import { prismaAdapter } from '@authcore/prisma-adapter'
import { PrismaClient } from '@prisma/client'

// Prisma 5 (default prisma-client-js generator):
const prisma = new PrismaClient()

// Prisma 6 (new prisma-client generator — requires a driver adapter):
// import { PrismaPg } from '@prisma/adapter-pg'
// const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL! })
// const prisma = new PrismaClient({ adapter })

@Module({
  imports: [
    AuthModule.register({
      db: prismaAdapter(prisma),
      session: { strategy: 'jwt', secret: process.env.AUTH_SECRET! },
    }),
  ],
})
export class AppModule {}

This registers all auth routes under /auth and makes guards available globally.

Protecting Routes

import { Controller, Get, UseGuards } from '@nestjs/common'
import { AuthGuard, CurrentUser } from '@authcore/nestjs'
import type { PublicUser } from '@authcore/nestjs'

@Controller('dashboard')
@UseGuards(AuthGuard)
export class DashboardController {
  @Get()
  getDashboard(@CurrentUser() user: PublicUser) {
    return { user }
  }
}

Role-Based Access Control

import { Controller, Get, UseGuards } from '@nestjs/common'
import { AuthGuard, RolesGuard, Roles, CurrentUser } from '@authcore/nestjs'
import type { PublicUser } from '@authcore/nestjs'

@Controller('admin')
@UseGuards(AuthGuard, RolesGuard)
@Roles('admin')
export class AdminController {
  @Get()
  getAdmin(@CurrentUser() user: PublicUser) {
    return { message: 'Admin area', user }
  }
}

Optional Authentication

import { Controller, Get, UseGuards } from '@nestjs/common'
import { AuthOptionalGuard, CurrentUser } from '@authcore/nestjs'
import type { PublicUser } from '@authcore/nestjs'

@Controller('public')
export class PublicController {
  @Get()
  @UseGuards(AuthOptionalGuard)
  getPublic(@CurrentUser() user: PublicUser | undefined) {
    return { user: user ?? null }
  }
}

API

AuthModule.register(options)

Creates a global NestJS module with all auth routes and providers. See @authcore/core for the full config reference.

Additional options:

| Option | Type | Default | Description | |--------|------|---------|-------------| | baseUrl | string | '' | Base URL for building email links | | useCookies | boolean | false | Set/clear an httpOnly cookie on register/login/logout/accept-invitation instead of returning the token in the body |

Cookie name comes from session.cookieName (default 'authcore_token'). See @authcore/types for the full SessionConfig shape.

Guards

| Guard | Description | |-------|-------------| | AuthGuard | Requires a valid JWT. Attaches request.user. Throws UnauthorizedException (401) if no token is present or the token is invalid. | | AuthOptionalGuard | Attaches request.user if token is valid. Never rejects. | | RolesGuard | Checks request.user.role against @Roles(). Throws ForbiddenException (403) if not allowed. Use after AuthGuard. |

Cookie Mode

// main.ts
import cookieParser from 'cookie-parser'
const app = await NestFactory.create(AppModule)
app.use(cookieParser())
// app.module.ts
AuthModule.register({
  db: prismaAdapter(prisma),
  session: {
    strategy: 'jwt',
    secret: process.env.AUTH_SECRET!,
    cookieName: 'my_token',
  },
  useCookies: true,
})

When useCookies: true:

  • POST /auth/register, POST /auth/login, POST /auth/accept-invitation set the cookie and return { user } (no token in body).
  • POST /auth/logout clears the cookie.
  • AuthGuard / AuthOptionalGuard fall back to reading the cookie if no Authorization: Bearer ... header is present.

Cookie mode requires @nestjs/platform-express (the default) and cookie-parser middleware in main.ts.

Decorators

| Decorator | Description | |-----------|-------------| | @CurrentUser() | Parameter decorator that extracts the authenticated user | | @Roles('admin', 'editor') | Sets allowed roles for a route or controller | | @Public() | Marks a route as public (skips AuthGuard) |

Routes

When mounted at /auth (default):

| Method | Route | Body | Response | |--------|-------|------|----------| | POST | /auth/register | { email, password } | { user, token } | | POST | /auth/login | { email, password } | { user, token } | | POST | /auth/logout | - | { message } | | GET | /auth/me | - | user | | POST | /auth/verify-email | { token } | { message } | | POST | /auth/forgot-password | { email } | { message } | | POST | /auth/reset-password | { token, password } | { message } | | POST | /auth/invite | { email, role? } | { message } | | POST | /auth/accept-invitation | { token, password } | { user, token } |

With Email Verification, Password Reset, and Invitation

import { resendAdapter } from '@authcore/resend-adapter'

AuthModule.register({
  db: prismaAdapter(prisma),
  session: { strategy: 'jwt', secret: process.env.AUTH_SECRET! },
  email: {
    provider: resendAdapter(process.env.RESEND_API_KEY!),
    from: '[email protected]',
  },
  features: ['emailVerification', 'passwordReset', 'invitation'],
  rbac: { defaultRole: 'user' },
})

License

MIT