chore: add config for enableTurnstileCaptcha

This commit is contained in:
javayhu 2025-06-30 23:50:56 +08:00
parent 55ae5ced9e
commit 3fa44f92c7
5 changed files with 18 additions and 12 deletions

View File

@ -16,7 +16,6 @@ import {
import { Input } from '@/components/ui/input';
import { websiteConfig } from '@/config/website';
import { authClient } from '@/lib/auth-client';
import { isTurnstileEnabled } from '@/lib/captcha';
import { getUrlWithLocaleInCallbackUrl } from '@/lib/urls/urls';
import { DEFAULT_LOGIN_REDIRECT, Routes } from '@/routes';
import { zodResolver } from '@hookform/resolvers/zod';
@ -54,7 +53,7 @@ export const RegisterForm = ({
const [showPassword, setShowPassword] = useState(false);
// turnstile captcha schema
const turnstileEnabled = isTurnstileEnabled();
const turnstileEnabled = websiteConfig.features.enableTurnstileCaptcha;
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 { isTurnstileEnabled } from '@/lib/captcha';
import { websiteConfig } from '@/config/website';
import { useLocale } from 'next-intl';
import { useTheme } from 'next-themes';
import dynamic from 'next/dynamic';
@ -22,7 +22,12 @@ type Props = Omit<ComponentProps<typeof Turnstile>, 'siteKey'> & {
* Captcha component for Cloudflare Turnstile
*/
export const Captcha = ({ validationError, ...props }: Props) => {
const turnstileEnabled = isTurnstileEnabled();
const turnstileEnabled = websiteConfig.features.enableTurnstileCaptcha;
if (!turnstileEnabled || !process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY) {
console.error('Captcha, NEXT_PUBLIC_TURNSTILE_SITE_KEY is not set');
return null;
}
const theme = useTheme();
const locale = useLocale();

View File

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

View File

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

View File

@ -71,6 +71,7 @@ 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
}
/**