Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

supertokens-nestjs

supertokens1.9kApache-2.00.0.5TypeScript support: included

SuperTokens integration for NestJS applications

supertokens, nestjs, nodejs, authentication, session-management

readme

SuperTokens banner

SuperTokens Nestjs

chat on Discord

Description

The library makes it easier to use SuperTokens in NestJS applications. It does that by exposing NestJS entities (modules, guards, decorators, etc.) which abstract some of the logic that a user would normally need to write by themselves. That being said, the library does not include any additional functionality besides what you can create with the SuperTokens Node SDK.

Usage

1. Install the dependencies

npm install supertokens-node supertokens-nestjs

2. Initialize the SuperTokensModule inside your main application module

import { Module } from '@nestjs/common'
import { SuperTokensModule } from 'supertokens-nestjs'

@Module({
  imports: [
    SuperTokensModule.forRoot({
      framework: 'express',
      supertokens: {
        connectionURI: '...',
      },
      appInfo: {
        appName: '...',
        apiDomain: '...',
        websiteDomain: '...',
      },
      recipeList: [
        /* ... */
      ],
    }),
  ],
  controllers: [
    /* ... */
  ],
  providers: [
    /* ... */
  ],
})
export class AppModule {}

3. Add the SuperTokensAuthGuard to protect your routes

Option 1: As a global guard

import { Module } from '@nestjs/common'
import { APP_GUARD } from '@nestjs/core'
import { SuperTokensAuthGuard } from 'supertokens-nestjs'

@Module({
  imports: [
    /* ... */
  ],
  controllers: [
    /* ... */
  ],
  providers: [
    {
      provide: APP_GUARD,
      useClass: SuperTokensAuthGuard,
    },
  ],
})
export class AppModule {}

Option 2: As a controller guard

import { Controller, UseGuards } from '@nestjs/common'
import { SuperTokensAuthGuard } from 'supertokens-nestjs'

@Controller()
@UseGuards(SuperTokensAuthGuard)
export class AppController {}

4. Add the CORS config and the exception filter in your bootstrap function

import supertokens from 'supertokens-node'
import { SuperTokensExceptionFilter } from 'supertokens-nestjs'
import { appInfo } from './config'

async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  app.enableCors({
    origin: [appInfo.websiteDomain],
    allowedHeaders: ['content-type', ...supertokens.getAllCORSHeaders()],
    credentials: true,
  })
  app.useGlobalFilters(new SuperTokensExceptionFilter())

  await app.listen(3001)
}

5. Use the provided decorators to customize the route protection logic and access the authentication state

import { Controller, Delete, Get, Patch, Post } from '@nestjs/common'
import { PublicAccess, Session, VerifySession } from 'supertokens-nestjs'

@Controller()
class AppController {
  @Get('/user')
  @VerifySession()
  async getUserInfo(@Session('userId') userId: string) {}

  @Get('/user/:userId')
  @VerifySession({
    roles: ['admin'],
  })
  async deleteUser() {}

  @Get('/user/profile')
  @PublicAccess()
  async getUserProfile() {}
}

changelog

Changelog

0.0.5 (2025-06-09)

Changes

  • Prevent dependency errors from being thrown when the auth guard is instantiated

0.0.4 (2025-05-29)

Changes

  • Added support for supertokens-node version 22.1.0 while maintaining compatibility with 21.1.0

0.0.3 (2025-05-09)

Changes

  • Removed express dependency
  • Fixed usage with graphql

0.0.2 (2025-03-05)

Changes

  • Initial release with NestJS abstractions
  • Support for Express and Fastify
  • Session management
  • User roles and permissions
  • Email verification
  • Multi-factor authentication