UNPKG

@mridang/nestjs-auth

Version:

A comprehensive Auth.js integration for NestJS applications with TypeScript support, framework-agnostic HTTP adapters, and role-based access control

130 lines (129 loc) 3.72 kB
import { DynamicModule } from '@nestjs/common'; import type { AuthModuleAsyncOptions, IAuthModuleOptions } from './auth-module.options.js'; /** * Configuration options for Auth.js module registration */ export interface AuthModuleConfig { /** * Whether to register a global authentication guard. * When true, all routes require authentication by default. * Use @Public() decorator to bypass authentication. * @default true */ globalGuard?: boolean; /** * Whether to register global roles guard. * When true, role-based authorization is enforced globally. * @default true */ rolesGuard?: boolean; /** * Base path for Auth.js routes * @default '/auth' */ basePath?: string; } /** * Main Auth.js module for NestJS applications. Provides authentication * infrastructure using Auth.js core with NestJS patterns and conventions. * * This module automatically registers: * - Auth.js routes (/auth/signin, /auth/callback, etc.) * - Global authentication guard (optional) * - Global authorization guard (optional) * - Session management middleware * * @example * **Basic Setup (Recommended)** * ```ts * @Module({ * imports: [ * AuthModule.register({ * providers: [ * GoogleProvider({ * clientId: process.env.GOOGLE_CLIENT_ID, * clientSecret: process.env.GOOGLE_CLIENT_SECRET, * }) * ], * secret: process.env.AUTH_SECRET, * trustHost: true * }) * ] * }) * export class AppModule {} * ``` * * @example * **Custom Configuration** * ```ts * @Module({ * imports: [ * AuthModule.register({ * providers: [GoogleProvider(...)], * secret: process.env.AUTH_SECRET, * trustHost: true * }, { * globalGuard: true, // Require auth by default * rolesGuard: true, // Enable role-based access * basePath: '/auth' // Auth routes a base path * }) * ] * }) * export class AppModule {} * ``` * * @example * **Using in Controllers** * ```ts * @Controller('api') * export class ApiController { * @Get('public') * @Public() // Bypass authentication * getPublicData() { * return { message: 'Public endpoint' }; * } * * @Get('profile') * // Authenticated by default (global guard) * getProfile(@AuthSession() session: Session | null) { * return { user: session?.user }; * } * * @Get('admin') * @RequireRoles('admin') // Role-based access * getAdminData(@AuthSession() session: Session | null) { * return { adminData: true }; * } * } * ``` */ export declare class AuthModule { /** * Register the Auth.js module with static configuration options. * * @param options - Static configuration options for Auth.js * @param config - Module configuration options * @returns A configured dynamic module */ static register(options: IAuthModuleOptions, config?: AuthModuleConfig): DynamicModule; /** * Register the Auth.js module with dynamic configuration options. * * @param options - Async configuration options for Auth.js * @param config - Module configuration options * @returns A configured dynamic module with async providers */ static registerAsync(options: AuthModuleAsyncOptions, config?: AuthModuleConfig): DynamicModule; /** * Creates guard providers based on configuration */ private static createGuardProviders; /** * Creates providers for async registration */ private static createAsyncProviders; /** * Creates the option provider for async configuration */ private static createAsyncOptionsProvider; }