chore: optimize turnstile captcha
This commit is contained in:
parent
46d008e5fc
commit
66567cfecd
@ -15,8 +15,8 @@ import {
|
|||||||
import { Input } from '@/components/ui/input';
|
import { Input } from '@/components/ui/input';
|
||||||
import { websiteConfig } from '@/config/website';
|
import { websiteConfig } from '@/config/website';
|
||||||
import { authClient } from '@/lib/auth-client';
|
import { authClient } from '@/lib/auth-client';
|
||||||
|
import { isTurnstileEnabled, validateTurnstileToken } from '@/lib/captcha';
|
||||||
import { getUrlWithLocaleInCallbackUrl } from '@/lib/urls/urls';
|
import { getUrlWithLocaleInCallbackUrl } from '@/lib/urls/urls';
|
||||||
import { validateTurnstileToken } from '@/lib/validate-captcha';
|
|
||||||
import { DEFAULT_LOGIN_REDIRECT, Routes } from '@/routes';
|
import { DEFAULT_LOGIN_REDIRECT, Routes } from '@/routes';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { EyeIcon, EyeOffIcon, Loader2Icon } from 'lucide-react';
|
import { EyeIcon, EyeOffIcon, Loader2Icon } from 'lucide-react';
|
||||||
@ -53,7 +53,7 @@ export const RegisterForm = ({
|
|||||||
const [showPassword, setShowPassword] = useState(false);
|
const [showPassword, setShowPassword] = useState(false);
|
||||||
|
|
||||||
// turnstile captcha schema
|
// turnstile captcha schema
|
||||||
const turnstileEnabled = websiteConfig.features.enableTurnstileCaptcha;
|
const turnstileEnabled = isTurnstileEnabled();
|
||||||
const captchaSchema = turnstileEnabled
|
const captchaSchema = turnstileEnabled
|
||||||
? z.string().min(1, 'Please complete the captcha')
|
? z.string().min(1, 'Please complete the captcha')
|
||||||
: z.string().optional();
|
: z.string().optional();
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { FormMessage } from '@/components/ui/form';
|
import { FormMessage } from '@/components/ui/form';
|
||||||
import { websiteConfig } from '@/config/website';
|
import { isTurnstileEnabled } from '@/lib/captcha';
|
||||||
import { useLocale } from 'next-intl';
|
import { useLocale } from 'next-intl';
|
||||||
import { useTheme } from 'next-themes';
|
import { useTheme } from 'next-themes';
|
||||||
import dynamic from 'next/dynamic';
|
import dynamic from 'next/dynamic';
|
||||||
@ -22,11 +22,11 @@ type Props = Omit<ComponentProps<typeof Turnstile>, 'siteKey'> & {
|
|||||||
* Captcha component for Cloudflare Turnstile
|
* Captcha component for Cloudflare Turnstile
|
||||||
*/
|
*/
|
||||||
export const Captcha = ({ validationError, ...props }: Props) => {
|
export const Captcha = ({ validationError, ...props }: Props) => {
|
||||||
const isTurnstileEnabled = websiteConfig.features.enableTurnstileCaptcha;
|
const turnstileEnabled = isTurnstileEnabled();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const locale = useLocale();
|
const locale = useLocale();
|
||||||
|
|
||||||
return isTurnstileEnabled ? (
|
return turnstileEnabled ? (
|
||||||
<>
|
<>
|
||||||
<Turnstile
|
<Turnstile
|
||||||
options={{
|
options={{
|
||||||
|
@ -38,7 +38,6 @@ export const websiteConfig: WebsiteConfig = {
|
|||||||
enableAffonsoAffiliate: false,
|
enableAffonsoAffiliate: false,
|
||||||
enablePromotekitAffiliate: false,
|
enablePromotekitAffiliate: false,
|
||||||
enableDatafastRevenueTrack: false,
|
enableDatafastRevenueTrack: false,
|
||||||
enableTurnstileCaptcha: true,
|
|
||||||
},
|
},
|
||||||
routes: {
|
routes: {
|
||||||
defaultLoginRedirect: '/dashboard',
|
defaultLoginRedirect: '/dashboard',
|
||||||
|
@ -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 {
|
interface TurnstileResponse {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
@ -9,8 +14,8 @@ interface TurnstileResponse {
|
|||||||
* https://developers.cloudflare.com/turnstile/get-started/server-side-validation/
|
* https://developers.cloudflare.com/turnstile/get-started/server-side-validation/
|
||||||
*/
|
*/
|
||||||
export async function validateTurnstileToken(token: string) {
|
export async function validateTurnstileToken(token: string) {
|
||||||
const isTurnstileEnabled = websiteConfig.features.enableTurnstileCaptcha;
|
const turnstileEnabled = isTurnstileEnabled();
|
||||||
if (!isTurnstileEnabled) {
|
if (!turnstileEnabled) {
|
||||||
console.log('validateTurnstileToken, turnstile is disabled');
|
console.log('validateTurnstileToken, turnstile is disabled');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
1
src/types/index.d.ts
vendored
1
src/types/index.d.ts
vendored
@ -71,7 +71,6 @@ export interface FeaturesConfig {
|
|||||||
enableAffonsoAffiliate?: boolean; // Whether to enable affonso affiliate
|
enableAffonsoAffiliate?: boolean; // Whether to enable affonso affiliate
|
||||||
enablePromotekitAffiliate?: boolean; // Whether to enable promotekit affiliate
|
enablePromotekitAffiliate?: boolean; // Whether to enable promotekit affiliate
|
||||||
enableDatafastRevenueTrack?: boolean; // Whether to enable datafast revenue tracking
|
enableDatafastRevenueTrack?: boolean; // Whether to enable datafast revenue tracking
|
||||||
enableTurnstileCaptcha?: boolean; // Whether to enable turnstile captcha
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user