Skip to main content

Captcha Service

CaptchaService is the main abstraction exported by nestjs-captcha-module.

It provides a consistent API for captcha verification, regardless of which provider is selected (e.g. RECAPTCHA).

Methods

MethodDescription
verifyAsync(captchaToken)Returns true if the captcha token is valid, otherwise false.

Usage example

captcha.controller.ts
import { Controller, Get, Headers, UnauthorizedException } from '@nestjs/common';
import { CaptchaService } from 'nestjs-captcha-module';

@Controller('captcha')
export class CaptchaController {
constructor(private readonly captchaService: CaptchaService) {}

@Get('verify')
async verify(@Headers('x-captcha-token') captchaToken?: string) {
if (!captchaToken) throw new UnauthorizedException('Captcha token is required');

const ok = await this.captchaService.verifyAsync(captchaToken);
if (!ok) throw new UnauthorizedException('Captcha verification failed');

return { ok };
}
}