Installation
Install the package
NPM
npm i nestjs-auth-module
Yarn
yarn add nestjs-auth-module
Configure your app module
You can register the module synchronously (register) or asynchronously (registerAsync).
| Field | Type | Required | Default | Notes |
|---|---|---|---|---|
provider | AuthProvider | ✅ Yes | — | Select the provider implementation to use. |
isGlobal | boolean | ❌ No | false | When true, registers the module as global in NestJS. |
AuthModule.register
app.module.ts
import { Module } from '@nestjs/common';
import { AuthModule, AuthProvider } from 'nestjs-auth-module';
@Module({
imports: [
AuthModule.register({
isGlobal: true,
provider: AuthProvider.JWT,
secret: process.env.AUTH_JWT_SECRET!,
defaultOptions: {
expiresIn: '30d',
},
}),
],
})
export class AppModule {}
AuthModule.registerAsync
In addition to the fields above, async registration supports NestJS imports, inject, and useFactory.
app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AuthModule, AuthProvider } from 'nestjs-auth-module';
@Module({
imports: [
ConfigModule.forRoot(),
AuthModule.registerAsync({
isGlobal: true,
provider: AuthProvider.JWT,
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
secret: config.get<string>('AUTH_JWT_SECRET')!,
defaultOptions: {
expiresIn: config.get<string>('AUTH_JWT_EXPIRES_IN') ?? '30d',
},
}),
}),
],
})
export class AppModule {}