chore: optimize turnstile captcha

This commit is contained in:
javayhu 2025-06-28 00:34:31 +08:00
parent 46d008e5fc
commit 66567cfecd
5 changed files with 13 additions and 10 deletions

View File

@ -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();

View File

@ -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<ComponentProps<typeof Turnstile>, '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 ? (
<>
<Turnstile
options={{

View File

@ -38,7 +38,6 @@ export const websiteConfig: WebsiteConfig = {
enableAffonsoAffiliate: false,
enablePromotekitAffiliate: false,
enableDatafastRevenueTrack: false,
enableTurnstileCaptcha: true,
},
routes: {
defaultLoginRedirect: '/dashboard',

View File

@ -1,4 +1,9 @@
import { websiteConfig } from '@/config/website';
export function isTurnstileEnabled() {
return (
process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY !== '' &&
process.env.TURNSTILE_SECRET_KEY !== ''
);
}
interface TurnstileResponse {
success: boolean;
@ -9,8 +14,8 @@ interface TurnstileResponse {
* https://developers.cloudflare.com/turnstile/get-started/server-side-validation/
*/
export async function validateTurnstileToken(token: string) {
const isTurnstileEnabled = websiteConfig.features.enableTurnstileCaptcha;
if (!isTurnstileEnabled) {
const turnstileEnabled = isTurnstileEnabled();
if (!turnstileEnabled) {
console.log('validateTurnstileToken, turnstile is disabled');
return true;
}

View File

@ -71,7 +71,6 @@ export interface FeaturesConfig {
enableAffonsoAffiliate?: boolean; // Whether to enable affonso affiliate
enablePromotekitAffiliate?: boolean; // Whether to enable promotekit affiliate
enableDatafastRevenueTrack?: boolean; // Whether to enable datafast revenue tracking
enableTurnstileCaptcha?: boolean; // Whether to enable turnstile captcha
}
/**