@mridang/nestjs-auth
Version:
A comprehensive Auth.js integration for NestJS applications with TypeScript support, framework-agnostic HTTP adapters, and role-based access control
187 lines • 6.15 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var AuthModule_1;
import { Module } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
import { AuthModuleOptions } from './auth-module.options.js';
import { AuthMiddleware } from './auth.middleware.js';
import { AuthGuard } from './auth.guards.js';
import { RolesGuard } from './roles.guard.js';
import { AuthController } from './auth.controller.js';
/**
* 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 };
* }
* }
* ```
*/
let AuthModule = AuthModule_1 = 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, config = {}) {
return this.registerAsync({
useFactory: () => options
}, config);
}
/**
* 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, config = {}) {
const asyncProviders = this.createAsyncProviders(options);
const guardProviders = this.createGuardProviders(config);
return {
module: AuthModule_1,
imports: options.imports ?? [],
controllers: [AuthController],
providers: [...asyncProviders, ...guardProviders],
exports: [AuthModuleOptions]
};
}
/**
* Creates guard providers based on configuration
*/
static createGuardProviders(config) {
const providers = [];
// Global authentication guard
if (config.globalGuard !== false) {
providers.push({
provide: APP_GUARD,
useClass: AuthGuard()
});
}
// Global roles guard
if (config.rolesGuard !== false) {
providers.push({
provide: APP_GUARD,
useClass: RolesGuard
});
}
return providers;
}
/**
* Creates providers for async registration
*/
static createAsyncProviders(options) {
const baseProviders = [AuthMiddleware];
if (options.useExisting || options.useFactory) {
return [...baseProviders, this.createAsyncOptionsProvider(options)];
}
if (!options.useClass) {
throw new Error('Invalid Auth.js module async options. Must provide one of: useFactory, useClass, or useExisting');
}
return [
...baseProviders,
this.createAsyncOptionsProvider(options),
{
provide: options.useClass,
useClass: options.useClass
}
];
}
/**
* Creates the option provider for async configuration
*/
static createAsyncOptionsProvider(options) {
if (options.useFactory) {
return {
provide: AuthModuleOptions,
useFactory: options.useFactory,
inject: [...(options.inject ?? [])]
};
}
const inject = options.useExisting ?? options.useClass;
if (!inject) {
throw new Error('Invalid Auth.js module async options. useClass or useExisting must be provided');
}
return {
provide: AuthModuleOptions,
useFactory: async (optionsFactory) => await optionsFactory.createAuthOptions(),
inject: [inject]
};
}
};
AuthModule = AuthModule_1 = __decorate([
Module({})
], AuthModule);
export { AuthModule };
//# sourceMappingURL=auth.module.js.map