in packages/better-auth/src/plugins/captcha/index.ts [11:76]
onRequest: async (request, ctx) => {
try {
const endpoints = options.endpoints?.length
? options.endpoints
: defaultEndpoints;
if (!endpoints.some((endpoint) => request.url.includes(endpoint)))
return undefined;
if (!options.secretKey) {
throw new Error(INTERNAL_ERROR_CODES.MISSING_SECRET_KEY);
}
const captchaResponse = request.headers.get("x-captcha-response");
const remoteUserIP =
request.headers.get("x-captcha-user-remote-ip") ?? undefined;
if (!captchaResponse) {
return middlewareResponse({
message: EXTERNAL_ERROR_CODES.MISSING_RESPONSE,
status: 400,
});
}
const siteVerifyURL =
options.siteVerifyURLOverride || siteVerifyMap[options.provider];
const handlerParams = {
siteVerifyURL,
captchaResponse,
secretKey: options.secretKey,
remoteIP: remoteUserIP,
};
if (options.provider === Providers.CLOUDFLARE_TURNSTILE) {
return await verifyHandlers.cloudflareTurnstile(handlerParams);
}
if (options.provider === Providers.GOOGLE_RECAPTCHA) {
return await verifyHandlers.googleRecaptcha({
...handlerParams,
minScore: options.minScore,
});
}
if (options.provider === Providers.HCAPTCHA) {
return await verifyHandlers.hCaptcha({
...handlerParams,
siteKey: options.siteKey,
});
}
} catch (_error) {
const errorMessage =
_error instanceof Error ? _error.message : undefined;
ctx.logger.error(errorMessage ?? "Unknown error", {
endpoint: request.url,
message: _error,
});
return middlewareResponse({
message: EXTERNAL_ERROR_CODES.UNKNOWN_ERROR,
status: 500,
});
}
},