Skip to main content

RecaptchaV2Provider

Package: @nauth-toolkit/recaptcha Type: Provider Class

Checkbox-based reCAPTCHA provider requiring explicit user interaction.

import { RecaptchaV2Provider } from '@nauth-toolkit/recaptcha';

Constructor

new RecaptchaV2Provider(config: RecaptchaV2Config)

RecaptchaV2Config

PropertyTypeRequiredDescription
secretKeystringYesSecret key from Google reCAPTCHA admin console.
timeoutnumberNoRequest timeout in milliseconds. Default: 10000.

Methods

verify()

Verify reCAPTCHA v2 token with Google's API.

async verify(token: string, remoteIp?: string): Promise<RecaptchaVerificationResult>

Parameters

  • token - reCAPTCHA token from client
  • remoteIp - Client IP address (optional, recommended)

Returns

  • RecaptchaVerificationResult - Verification result with success status

validateConfig()

Validate provider credentials at startup by sending a probe request to Google's API.

async validateConfig(): Promise<RecaptchaValidationResult>

Returns

  • RecaptchaValidationResult - { valid, message, hint?, httpStatus? }

Called automatically during NAuth.create() when validateOnStartup is 'warn' (default) or 'error'. Detects invalid secret keys before a real user hits the endpoint.

Example

import { NAuthModule } from '@nauth-toolkit/nestjs';
import { RecaptchaV2Provider } from '@nauth-toolkit/recaptcha';

@Module({
imports: [
NAuthModule.forRoot({
recaptcha: {
enabled: true,
provider: new RecaptchaV2Provider({
secretKey: process.env.RECAPTCHA_V2_SECRET_KEY!,
}),
},
}),
],
})
export class AppModule {}

When to Use

  • Acceptable user friction (checkbox interaction)
  • Simple bot protection without scoring
  • Lower setup complexity than v3/Enterprise

Setup

  1. Go to Google reCAPTCHA Admin Console
  2. Create a new site with reCAPTCHA v2 checkbox
  3. Add your domains (including localhost for development)
  4. Copy the secret key for backend configuration
  5. Copy the site key for frontend integration