Skip to main content

Installation

Install the package

NPM

npm i nestjs-captcha-module

Yarn

yarn add nestjs-captcha-module

Configure your app module

You can register the module synchronously (register) or asynchronously (registerAsync).

FieldTypeRequiredDefaultNotes
providerCaptchaProvider✅ YesSelect the provider implementation to use.
isGlobalboolean❌ NofalseWhen true, registers the module as global in NestJS.
guardOptions.location'headers' | 'body'❌ No'headers'Where to read the captcha token from.
guardOptions.fieldNamestring❌ No"x-captcha-token"Header/body field name for the captcha token.

CaptchaModule.register

app.module.ts
import { Module } from '@nestjs/common';
import { CaptchaModule, CaptchaProvider } from 'nestjs-captcha-module';

@Module({
imports: [
CaptchaModule.register({
isGlobal: true,
provider: CaptchaProvider.RECAPTCHA,
secretKey: process.env.RECAPTCHA_SECRET_KEY!,
guardOptions: {
location: 'headers',
fieldName: 'x-captcha-token',
},
}),
],
})
export class AppModule {}

CaptchaModule.registerAsync

app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { CaptchaModule, CaptchaProvider } from 'nestjs-captcha-module';

@Module({
imports: [
ConfigModule.forRoot(),

CaptchaModule.registerAsync({
isGlobal: true,
provider: CaptchaProvider.RECAPTCHA,
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
secretKey: config.get<string>('RECAPTCHA_SECRET_KEY')!,
guardOptions: {
location: 'headers',
fieldName: 'x-captcha-token',
},
}),
}),
],
})
export class AppModule {}