From 66567cfecdf5e9ea29ad2cbd866269b013cdf3ef Mon Sep 17 00:00:00 2001 From: javayhu Date: Sat, 28 Jun 2025 00:34:31 +0800 Subject: [PATCH] chore: optimize turnstile captcha --- src/components/auth/register-form.tsx | 4 ++-- src/components/shared/captcha.tsx | 6 +++--- src/config/website.tsx | 1 - src/lib/{validate-captcha.ts => captcha.ts} | 11 ++++++++--- src/types/index.d.ts | 1 - 5 files changed, 13 insertions(+), 10 deletions(-) rename src/lib/{validate-captcha.ts => captcha.ts} (75%) diff --git a/src/components/auth/register-form.tsx b/src/components/auth/register-form.tsx index 66bb123..c00878e 100644 --- a/src/components/auth/register-form.tsx +++ b/src/components/auth/register-form.tsx @@ -15,8 +15,8 @@ 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 { getUrlWithLocaleInCallbackUrl } from '@/lib/urls/urls'; -import { validateTurnstileToken } from '@/lib/validate-captcha'; import { DEFAULT_LOGIN_REDIRECT, Routes } from '@/routes'; import { zodResolver } from '@hookform/resolvers/zod'; import { EyeIcon, EyeOffIcon, Loader2Icon } from 'lucide-react'; @@ -53,7 +53,7 @@ export const RegisterForm = ({ const [showPassword, setShowPassword] = useState(false); // turnstile captcha schema - const turnstileEnabled = websiteConfig.features.enableTurnstileCaptcha; + const turnstileEnabled = isTurnstileEnabled(); const captchaSchema = turnstileEnabled ? z.string().min(1, 'Please complete the captcha') : z.string().optional(); diff --git a/src/components/shared/captcha.tsx b/src/components/shared/captcha.tsx index 3417d3a..ae1ee2b 100644 --- a/src/components/shared/captcha.tsx +++ b/src/components/shared/captcha.tsx @@ -1,7 +1,7 @@ 'use client'; import { FormMessage } from '@/components/ui/form'; -import { websiteConfig } from '@/config/website'; +import { isTurnstileEnabled } from '@/lib/captcha'; import { useLocale } from 'next-intl'; import { useTheme } from 'next-themes'; import dynamic from 'next/dynamic'; @@ -22,11 +22,11 @@ type Props = Omit, 'siteKey'> & { * Captcha component for Cloudflare Turnstile */ export const Captcha = ({ validationError, ...props }: Props) => { - const isTurnstileEnabled = websiteConfig.features.enableTurnstileCaptcha; + const turnstileEnabled = isTurnstileEnabled(); const theme = useTheme(); const locale = useLocale(); - return isTurnstileEnabled ? ( + return turnstileEnabled ? ( <>