B2B-88: add starter kit structure and elements
This commit is contained in:
1
packages/features/auth/src/captcha/server/index.ts
Normal file
1
packages/features/auth/src/captcha/server/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './verify-captcha';
|
||||
39
packages/features/auth/src/captcha/server/verify-captcha.tsx
Normal file
39
packages/features/auth/src/captcha/server/verify-captcha.tsx
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'server-only';
|
||||
|
||||
const verifyEndpoint =
|
||||
'https://challenges.cloudflare.com/turnstile/v0/siteverify';
|
||||
|
||||
const CAPTCHA_SECRET_TOKEN = process.env.CAPTCHA_SECRET_TOKEN;
|
||||
|
||||
/**
|
||||
* @name verifyCaptchaToken
|
||||
* @description Verify the CAPTCHA token with the CAPTCHA service
|
||||
* @param token - The CAPTCHA token to verify
|
||||
*/
|
||||
export async function verifyCaptchaToken(token: string) {
|
||||
if (!CAPTCHA_SECRET_TOKEN) {
|
||||
throw new Error('CAPTCHA_SECRET_TOKEN is not set');
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
|
||||
formData.append('secret', CAPTCHA_SECRET_TOKEN);
|
||||
formData.append('response', token);
|
||||
|
||||
const res = await fetch(verifyEndpoint, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
console.error(`Captcha failed:`, res.statusText);
|
||||
|
||||
throw new Error('Failed to verify CAPTCHA token');
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
|
||||
if (!data.success) {
|
||||
throw new Error('Invalid CAPTCHA token');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user