148 lines
4.5 KiB
TypeScript
148 lines
4.5 KiB
TypeScript
import useGlobalStore from '@/config/use-global';
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
import { AiroButton } from '@workspace/airo-ui/components/AiroButton';
|
|
import { Button } from '@workspace/airo-ui/components/button';
|
|
import {
|
|
Form,
|
|
FormControl,
|
|
FormField,
|
|
FormItem,
|
|
FormMessage,
|
|
} from '@workspace/airo-ui/components/form';
|
|
import { Input } from '@workspace/airo-ui/components/input';
|
|
import { Icon } from '@workspace/airo-ui/custom-components/icon';
|
|
import { useTranslations } from 'next-intl';
|
|
import { Dispatch, SetStateAction, useRef } from 'react';
|
|
import { useForm } from 'react-hook-form';
|
|
import { z } from 'zod';
|
|
import CloudFlareTurnstile, { TurnstileRef } from '../turnstile';
|
|
|
|
export default function LoginForm({
|
|
loading,
|
|
onSubmit,
|
|
initialValues,
|
|
setInitialValues,
|
|
onSwitchForm,
|
|
}: {
|
|
loading?: boolean;
|
|
onSubmit: (data: any) => void;
|
|
initialValues: any;
|
|
setInitialValues: Dispatch<SetStateAction<any>>;
|
|
onSwitchForm: Dispatch<SetStateAction<'register' | 'reset' | 'login'>>;
|
|
}) {
|
|
const t = useTranslations('auth.login');
|
|
const { common } = useGlobalStore();
|
|
const { verify } = common;
|
|
|
|
const formSchema = z.object({
|
|
email: z.string().email(t('email')),
|
|
password: z.string(),
|
|
cf_token:
|
|
verify.enable_login_verify && verify.turnstile_site_key ? z.string() : z.string().optional(),
|
|
});
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
resolver: zodResolver(formSchema),
|
|
defaultValues: initialValues,
|
|
});
|
|
|
|
const turnstile = useRef<TurnstileRef>(null);
|
|
const handleSubmit = form.handleSubmit((data) => {
|
|
try {
|
|
onSubmit(data);
|
|
} catch (error) {
|
|
turnstile.current?.reset();
|
|
}
|
|
});
|
|
|
|
return (
|
|
<>
|
|
<div className={'h-[84px] text-2xl font-bold leading-[84px]'}>账户验证</div>
|
|
<Form {...form}>
|
|
<form onSubmit={handleSubmit} className=''>
|
|
<FormField
|
|
control={form.control}
|
|
name='email'
|
|
render={({ field }) => (
|
|
<FormItem className={'mb-5'}>
|
|
<FormControl>
|
|
<Input
|
|
className={'h-[46px] rounded-full shadow-[inset_0_0_7.6px_0_#00000040]'}
|
|
placeholder='Email'
|
|
type='email'
|
|
{...field}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name='password'
|
|
render={({ field }) => (
|
|
<FormItem className={'mb-2'}>
|
|
<FormControl>
|
|
<Input
|
|
className={'h-[46px] rounded-full shadow-[inset_0_0_7.6px_0_#00000040]'}
|
|
placeholder='Password'
|
|
type='password'
|
|
{...field}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
{verify.enable_login_verify && (
|
|
<FormField
|
|
control={form.control}
|
|
name='cf_token'
|
|
render={({ field }) => (
|
|
<FormItem className={'last:mb-0'}>
|
|
<FormControl>
|
|
<CloudFlareTurnstile id='login' {...field} ref={turnstile} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
)}
|
|
<div className='flex w-full justify-between text-sm'>
|
|
<Button
|
|
variant='link'
|
|
type='button'
|
|
className='p-0 text-[#225BA9]'
|
|
onClick={() => onSwitchForm('reset')}
|
|
>
|
|
{t('forgotPassword')}
|
|
</Button>
|
|
<Button
|
|
variant='link'
|
|
type='button'
|
|
className='p-0 text-[#225BA9]'
|
|
onClick={() => {
|
|
setInitialValues(undefined);
|
|
onSwitchForm('register');
|
|
}}
|
|
>
|
|
{t('registerAccount')}
|
|
</Button>
|
|
</div>
|
|
|
|
<div className='mt-6 flex justify-center'>
|
|
<AiroButton
|
|
type='submit'
|
|
variant='default'
|
|
disabled={loading}
|
|
className='h-auto min-w-[157px] py-2 text-lg font-medium'
|
|
>
|
|
{loading && <Icon icon='mdi:loading' className='animate-spin' />}
|
|
{t('title')}
|
|
</AiroButton>
|
|
</div>
|
|
</form>
|
|
</Form>
|
|
</>
|
|
);
|
|
}
|