'use client' import { infer as zInfer } from 'zod' import { useState, useTransition } from 'react' import { useForm } from 'react-hook-form' import { useSearchParams } from 'next/navigation' import { zodResolver } from '@hookform/resolvers/zod' import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form' import { Input } from '@/components/ui/input' import { CardWrapper } from '@/components/auth/card-wrapper' import { useI18n } from '@/locales/client' import { Button } from '@/components/ui/button' import FormError from '@/components/form-error' import FormSuccess from '@/components/form-success' import { login } from '@/actions/login' import { LoginSchema } from '@/schemas' import { AUTH_REGISTER_URL, AUTH_RESET_PASSWORD_URL } from '@/config/routes' import Link from 'next/link' export const LoginForm = () => { const t = useI18n() const searchParams = useSearchParams() const urlError = searchParams.get('error') === 'OAuthAccountNotLinked' ? t('auth.form.error.email_in_use') : '' const [showTwoFactor, setShowTwoFactor] = useState(false) const [error, setError] = useState('') const [success, setSuccess] = useState('') const [isPending, startTransition] = useTransition() const form = useForm>({ resolver: zodResolver(LoginSchema), defaultValues: { email: '', password: '', }, }) const onSubmit = (values: zInfer) => { setError('') setSuccess('') startTransition(() => { login(values).then((data) => { //@ts-ignore if (data?.error) { form.reset() //@ts-ignore setError(t(data?.error)) } //@ts-ignore if (data?.success) { form.reset() //@ts-ignore setSuccess(t(data?.success)) } //@ts-ignore if (data?.twoFactor) { //@ts-ignore setShowTwoFactor(data?.twoFactor) } }).catch((err) => { setError('auth.common.something_went_wrong') //TODO: do logging console.log(err) }) }) } return (
{showTwoFactor && ( ( {t('form.label.two_factor')} )}/> )} {!showTwoFactor && (<> ( {t('form.label.email')} )}/> ( {t('form.label.password')} )}/> )}
) }