RecaptchaV2Provider
Package: @nauth-toolkit/recaptcha
Type: Provider Class
Checkbox-based reCAPTCHA provider requiring explicit user interaction.
- NestJS
- Express
- Fastify
import { RecaptchaV2Provider } from '@nauth-toolkit/recaptcha';
import { RecaptchaV2Provider } from '@nauth-toolkit/recaptcha';
import { RecaptchaV2Provider } from '@nauth-toolkit/recaptcha';
Constructor
new RecaptchaV2Provider(config: RecaptchaV2Config)
RecaptchaV2Config
| Property | Type | Required | Description |
|---|---|---|---|
secretKey | string | Yes | Secret key from Google reCAPTCHA admin console. |
timeout | number | No | Request 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 clientremoteIp- 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
- NestJS
- Express
- Fastify
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 {}
import { NAuth } from '@nauth-toolkit/core';
import { ExpressAdapter } from '@nauth-toolkit/express';
import { RecaptchaV2Provider } from '@nauth-toolkit/recaptcha';
const nauth = await NAuth.create({
config: {
recaptcha: {
enabled: true,
provider: new RecaptchaV2Provider({
secretKey: process.env.RECAPTCHA_V2_SECRET_KEY!,
}),
},
},
dataSource,
adapter: new ExpressAdapter(),
});
import { NAuth } from '@nauth-toolkit/core';
import { FastifyAdapter } from '@nauth-toolkit/fastify';
import { RecaptchaV2Provider } from '@nauth-toolkit/recaptcha';
const nauth = await NAuth.create({
config: {
recaptcha: {
enabled: true,
provider: new RecaptchaV2Provider({
secretKey: process.env.RECAPTCHA_V2_SECRET_KEY!,
}),
},
},
dataSource,
adapter: new FastifyAdapter(),
});
When to Use
- Acceptable user friction (checkbox interaction)
- Simple bot protection without scoring
- Lower setup complexity than v3/Enterprise
Setup
- Go to Google reCAPTCHA Admin Console
- Create a new site with reCAPTCHA v2 checkbox
- Add your domains (including
localhostfor development) - Copy the secret key for backend configuration
- Copy the site key for frontend integration
Related
- RecaptchaConfig - Configuration interface
- RecaptchaV3Provider - Score-based alternative
- reCAPTCHA Guide - Complete implementation guide