🐛 fix(auth): Refactor forms to use Turnstile ref for reset functionality
This commit is contained in:
parent
42ba9e88ad
commit
320a7dc342
@ -5,10 +5,10 @@ import { Form, FormControl, FormField, FormItem, FormMessage } from '@workspace/
|
|||||||
import { Input } from '@workspace/ui/components/input';
|
import { Input } from '@workspace/ui/components/input';
|
||||||
import { Icon } from '@workspace/ui/custom-components/icon';
|
import { Icon } from '@workspace/ui/custom-components/icon';
|
||||||
import { useTranslations } from 'next-intl';
|
import { useTranslations } from 'next-intl';
|
||||||
import { Dispatch, SetStateAction } from 'react';
|
import { Dispatch, SetStateAction, useRef } from 'react';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import CloudFlareTurnstile from '../turnstile';
|
import CloudFlareTurnstile, { TurnstileRef } from '../turnstile';
|
||||||
|
|
||||||
export default function LoginForm({
|
export default function LoginForm({
|
||||||
loading,
|
loading,
|
||||||
@ -38,10 +38,16 @@ export default function LoginForm({
|
|||||||
defaultValues: initialValues,
|
defaultValues: initialValues,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const turnstile = useRef<TurnstileRef>(null);
|
||||||
|
const handleSubmit = form.handleSubmit((data) => {
|
||||||
|
onSubmit(data);
|
||||||
|
turnstile.current?.reset();
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)} className='grid gap-6'>
|
<form onSubmit={handleSubmit} className='grid gap-6'>
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name='email'
|
name='email'
|
||||||
@ -73,7 +79,7 @@ export default function LoginForm({
|
|||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<CloudFlareTurnstile id='login' {...field} />
|
<CloudFlareTurnstile id='login' {...field} ref={turnstile} />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
|
|||||||
@ -6,11 +6,11 @@ import { Input } from '@workspace/ui/components/input';
|
|||||||
import { Icon } from '@workspace/ui/custom-components/icon';
|
import { Icon } from '@workspace/ui/custom-components/icon';
|
||||||
import { Markdown } from '@workspace/ui/custom-components/markdown';
|
import { Markdown } from '@workspace/ui/custom-components/markdown';
|
||||||
import { useTranslations } from 'next-intl';
|
import { useTranslations } from 'next-intl';
|
||||||
import { Dispatch, SetStateAction } from 'react';
|
import { Dispatch, SetStateAction, useRef } from 'react';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import SendCode from '../send-code';
|
import SendCode from '../send-code';
|
||||||
import CloudFlareTurnstile from '../turnstile';
|
import CloudFlareTurnstile, { TurnstileRef } from '../turnstile';
|
||||||
|
|
||||||
export default function RegisterForm({
|
export default function RegisterForm({
|
||||||
loading,
|
loading,
|
||||||
@ -76,13 +76,19 @@ export default function RegisterForm({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const turnstile = useRef<TurnstileRef>(null);
|
||||||
|
const handleSubmit = form.handleSubmit((data) => {
|
||||||
|
onSubmit(data);
|
||||||
|
turnstile.current?.reset();
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{auth.register.stop_register ? (
|
{auth.register.stop_register ? (
|
||||||
<Markdown>{t('message')}</Markdown>
|
<Markdown>{t('message')}</Markdown>
|
||||||
) : (
|
) : (
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)} className='grid gap-6'>
|
<form onSubmit={handleSubmit} className='grid gap-6'>
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name='email'
|
name='email'
|
||||||
@ -177,7 +183,7 @@ export default function RegisterForm({
|
|||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<CloudFlareTurnstile id='register' {...field} />
|
<CloudFlareTurnstile id='register' {...field} ref={turnstile} />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
|
|||||||
@ -5,11 +5,11 @@ import { Form, FormControl, FormField, FormItem, FormMessage } from '@workspace/
|
|||||||
import { Input } from '@workspace/ui/components/input';
|
import { Input } from '@workspace/ui/components/input';
|
||||||
import { Icon } from '@workspace/ui/custom-components/icon';
|
import { Icon } from '@workspace/ui/custom-components/icon';
|
||||||
import { useTranslations } from 'next-intl';
|
import { useTranslations } from 'next-intl';
|
||||||
import { Dispatch, SetStateAction } from 'react';
|
import { Dispatch, SetStateAction, useRef } from 'react';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import SendCode from '../send-code';
|
import SendCode from '../send-code';
|
||||||
import CloudFlareTurnstile from '../turnstile';
|
import CloudFlareTurnstile, { TurnstileRef } from '../turnstile';
|
||||||
|
|
||||||
export default function ResetForm({
|
export default function ResetForm({
|
||||||
loading,
|
loading,
|
||||||
@ -43,10 +43,16 @@ export default function ResetForm({
|
|||||||
defaultValues: initialValues,
|
defaultValues: initialValues,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const turnstile = useRef<TurnstileRef>(null);
|
||||||
|
const handleSubmit = form.handleSubmit((data) => {
|
||||||
|
onSubmit(data);
|
||||||
|
turnstile.current?.reset();
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)} className='grid gap-6'>
|
<form onSubmit={handleSubmit} className='grid gap-6'>
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name='email'
|
name='email'
|
||||||
@ -105,7 +111,7 @@ export default function ResetForm({
|
|||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<CloudFlareTurnstile id='reset' {...field} />
|
<CloudFlareTurnstile id='reset' {...field} ref={turnstile} />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
|
|||||||
@ -7,11 +7,11 @@ import { AreaCodeSelect } from '@workspace/ui/custom-components/area-code-select
|
|||||||
import { Icon } from '@workspace/ui/custom-components/icon';
|
import { Icon } from '@workspace/ui/custom-components/icon';
|
||||||
|
|
||||||
import { useTranslations } from 'next-intl';
|
import { useTranslations } from 'next-intl';
|
||||||
import { Dispatch, SetStateAction, useState } from 'react';
|
import { Dispatch, SetStateAction, useRef, useState } from 'react';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import SendCode from '../send-code';
|
import SendCode from '../send-code';
|
||||||
import CloudFlareTurnstile from '../turnstile';
|
import CloudFlareTurnstile, { TurnstileRef } from '../turnstile';
|
||||||
|
|
||||||
export default function LoginForm({
|
export default function LoginForm({
|
||||||
loading,
|
loading,
|
||||||
@ -45,10 +45,16 @@ export default function LoginForm({
|
|||||||
|
|
||||||
const [mode, setMode] = useState<'password' | 'code'>('password');
|
const [mode, setMode] = useState<'password' | 'code'>('password');
|
||||||
|
|
||||||
|
const turnstile = useRef<TurnstileRef>(null);
|
||||||
|
const handleSubmit = form.handleSubmit((data) => {
|
||||||
|
onSubmit(data);
|
||||||
|
turnstile.current?.reset();
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)} className='grid gap-6'>
|
<form onSubmit={handleSubmit} className='grid gap-6'>
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name='telephone'
|
name='telephone'
|
||||||
@ -138,7 +144,7 @@ export default function LoginForm({
|
|||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<CloudFlareTurnstile id='login' {...field} />
|
<CloudFlareTurnstile id='login' {...field} ref={turnstile} />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
|
|||||||
@ -7,11 +7,11 @@ import { AreaCodeSelect } from '@workspace/ui/custom-components/area-code-select
|
|||||||
import { Icon } from '@workspace/ui/custom-components/icon';
|
import { Icon } from '@workspace/ui/custom-components/icon';
|
||||||
import { Markdown } from '@workspace/ui/custom-components/markdown';
|
import { Markdown } from '@workspace/ui/custom-components/markdown';
|
||||||
import { useTranslations } from 'next-intl';
|
import { useTranslations } from 'next-intl';
|
||||||
import { Dispatch, SetStateAction } from 'react';
|
import { Dispatch, SetStateAction, useRef } from 'react';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import SendCode from '../send-code';
|
import SendCode from '../send-code';
|
||||||
import CloudFlareTurnstile from '../turnstile';
|
import CloudFlareTurnstile, { TurnstileRef } from '../turnstile';
|
||||||
|
|
||||||
export default function RegisterForm({
|
export default function RegisterForm({
|
||||||
loading,
|
loading,
|
||||||
@ -63,13 +63,19 @@ export default function RegisterForm({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const turnstile = useRef<TurnstileRef>(null);
|
||||||
|
const handleSubmit = form.handleSubmit((data) => {
|
||||||
|
onSubmit(data);
|
||||||
|
turnstile.current?.reset();
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{auth.register.stop_register ? (
|
{auth.register.stop_register ? (
|
||||||
<Markdown>{t('message')}</Markdown>
|
<Markdown>{t('message')}</Markdown>
|
||||||
) : (
|
) : (
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)} className='grid gap-6'>
|
<form onSubmit={handleSubmit} className='grid gap-6'>
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name='telephone'
|
name='telephone'
|
||||||
@ -193,7 +199,7 @@ export default function RegisterForm({
|
|||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<CloudFlareTurnstile id='register' {...field} />
|
<CloudFlareTurnstile id='register' {...field} ref={turnstile} />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
|
|||||||
@ -6,11 +6,11 @@ import { Input } from '@workspace/ui/components/input';
|
|||||||
import { AreaCodeSelect } from '@workspace/ui/custom-components/area-code-select';
|
import { AreaCodeSelect } from '@workspace/ui/custom-components/area-code-select';
|
||||||
import { Icon } from '@workspace/ui/custom-components/icon';
|
import { Icon } from '@workspace/ui/custom-components/icon';
|
||||||
import { useTranslations } from 'next-intl';
|
import { useTranslations } from 'next-intl';
|
||||||
import { Dispatch, SetStateAction, useState } from 'react';
|
import { Dispatch, SetStateAction, useRef, useState } from 'react';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import SendCode from '../send-code';
|
import SendCode from '../send-code';
|
||||||
import CloudFlareTurnstile from '../turnstile';
|
import CloudFlareTurnstile, { TurnstileRef } from '../turnstile';
|
||||||
|
|
||||||
export default function ResetForm({
|
export default function ResetForm({
|
||||||
loading,
|
loading,
|
||||||
@ -46,10 +46,16 @@ export default function ResetForm({
|
|||||||
defaultValues: initialValues,
|
defaultValues: initialValues,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const turnstile = useRef<TurnstileRef>(null);
|
||||||
|
const handleSubmit = form.handleSubmit((data) => {
|
||||||
|
onSubmit(data);
|
||||||
|
turnstile.current?.reset();
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)} className='grid gap-6'>
|
<form onSubmit={handleSubmit} className='grid gap-6'>
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name='telephone'
|
name='telephone'
|
||||||
@ -136,7 +142,7 @@ export default function ResetForm({
|
|||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<CloudFlareTurnstile id='reset' {...field} />
|
<CloudFlareTurnstile id='reset' {...field} ref={turnstile} />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
|
|||||||
@ -2,26 +2,37 @@
|
|||||||
|
|
||||||
import { useLocale } from 'next-intl';
|
import { useLocale } from 'next-intl';
|
||||||
import { useTheme } from 'next-themes';
|
import { useTheme } from 'next-themes';
|
||||||
import { useEffect } from 'react';
|
import { forwardRef, useEffect, useImperativeHandle } from 'react';
|
||||||
import Turnstile, { useTurnstile } from 'react-turnstile';
|
import Turnstile, { useTurnstile } from 'react-turnstile';
|
||||||
|
|
||||||
import useGlobalStore from '@/config/use-global';
|
import useGlobalStore from '@/config/use-global';
|
||||||
|
|
||||||
export default function CloudFlareTurnstile({
|
export type TurnstileRef = {
|
||||||
id,
|
reset: () => void;
|
||||||
value,
|
};
|
||||||
onChange,
|
|
||||||
}: {
|
const CloudFlareTurnstile = forwardRef<
|
||||||
id?: string;
|
TurnstileRef,
|
||||||
value?: null | string;
|
{
|
||||||
onChange: (value?: string) => void;
|
id?: string;
|
||||||
}) {
|
value?: null | string;
|
||||||
|
onChange: (value?: string) => void;
|
||||||
|
}
|
||||||
|
>(function CloudFlareTurnstile({ id, value, onChange }, ref) {
|
||||||
const { common } = useGlobalStore();
|
const { common } = useGlobalStore();
|
||||||
const { verify } = common;
|
const { verify } = common;
|
||||||
const { resolvedTheme } = useTheme();
|
const { resolvedTheme } = useTheme();
|
||||||
const locale = useLocale();
|
const locale = useLocale();
|
||||||
const turnstile = useTurnstile();
|
const turnstile = useTurnstile();
|
||||||
|
|
||||||
|
useImperativeHandle(
|
||||||
|
ref,
|
||||||
|
() => ({
|
||||||
|
reset: () => turnstile.reset(),
|
||||||
|
}),
|
||||||
|
[turnstile],
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (value === '') {
|
if (value === '') {
|
||||||
turnstile.reset();
|
turnstile.reset();
|
||||||
@ -52,4 +63,6 @@ export default function CloudFlareTurnstile({
|
|||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
});
|
||||||
|
|
||||||
|
export default CloudFlareTurnstile;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user