From 05b90fb0a77a297b3cf6f3bf820f1ef9eaca9acc Mon Sep 17 00:00:00 2001 From: javayhu Date: Sat, 28 Jun 2025 10:09:50 +0800 Subject: [PATCH] chore: optimize validate captcha --- src/actions/validate-captcha.ts | 36 +++++++++++++++++++++++++++ src/components/auth/register-form.tsx | 23 ++++++++--------- 2 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 src/actions/validate-captcha.ts diff --git a/src/actions/validate-captcha.ts b/src/actions/validate-captcha.ts new file mode 100644 index 0000000..691f3f5 --- /dev/null +++ b/src/actions/validate-captcha.ts @@ -0,0 +1,36 @@ +'use server'; + +import { validateTurnstileToken } from '@/lib/captcha'; +import { createSafeActionClient } from 'next-safe-action'; +import { z } from 'zod'; + +// Create a safe action client +const actionClient = createSafeActionClient(); + +// Captcha validation schema +const captchaSchema = z.object({ + captchaToken: z.string().min(1, { message: 'Captcha token is required' }), +}); + +// Create a safe action for captcha validation +export const validateCaptchaAction = actionClient + .schema(captchaSchema) + .action(async ({ parsedInput }) => { + const { captchaToken } = parsedInput; + + try { + const isValid = await validateTurnstileToken(captchaToken); + + return { + success: true, + valid: isValid, + }; + } catch (error) { + console.error('Captcha validation error:', error); + return { + success: false, + valid: false, + error: error instanceof Error ? error.message : 'Something went wrong', + }; + } + }); diff --git a/src/components/auth/register-form.tsx b/src/components/auth/register-form.tsx index c00878e..21c0f77 100644 --- a/src/components/auth/register-form.tsx +++ b/src/components/auth/register-form.tsx @@ -1,5 +1,6 @@ 'use client'; +import { validateCaptchaAction } from '@/actions/validate-captcha'; import { AuthCard } from '@/components/auth/auth-card'; import { FormError } from '@/components/shared/form-error'; import { FormSuccess } from '@/components/shared/form-success'; @@ -15,7 +16,7 @@ import { import { Input } from '@/components/ui/input'; import { websiteConfig } from '@/config/website'; import { authClient } from '@/lib/auth-client'; -import { isTurnstileEnabled, validateTurnstileToken } from '@/lib/captcha'; +import { isTurnstileEnabled } from '@/lib/captcha'; import { getUrlWithLocaleInCallbackUrl } from '@/lib/urls/urls'; import { DEFAULT_LOGIN_REDIRECT, Routes } from '@/routes'; import { zodResolver } from '@hookform/resolvers/zod'; @@ -89,18 +90,14 @@ export const RegisterForm = ({ const onSubmit = async (values: z.infer) => { // Validate captcha token if turnstile is enabled if (turnstileEnabled && values.captchaToken) { - try { - const isCaptchaValid = await validateTurnstileToken( - values.captchaToken - ); - if (!isCaptchaValid) { - console.log('register, captcha invalid:', values.captchaToken); - setError(t('captchaInvalid') || 'Captcha verification failed'); - return; - } - } catch (error) { - console.error('register, captcha validation error:', error); - setError(t('captchaError') || 'Captcha verification error'); + const captchaResult = await validateCaptchaAction({ + captchaToken: values.captchaToken, + }); + + if (!captchaResult?.data?.success || !captchaResult?.data?.valid) { + console.error('register, captcha invalid:', values.captchaToken); + const errorMessage = captchaResult?.data?.error || t('captchaInvalid'); + setError(errorMessage); return; } }