import { zodResolver } from '@hookform/resolvers/zod' import { useForm } from 'react-hook-form' import { useTranslation } from 'react-i18next' import { z } from 'zod' import { toast } from '@/shared/ui/toast-store' import { Button } from '@/shared/ui/button' import { Input } from '@/shared/ui/input' import { Label } from '@/shared/ui/label' import { HttpError } from '@/shared/api/client' import { login, register as registerUser, applyAuthResponse } from './api' const schema = z.object({ userName: z .string() .min(3) .max(32) .regex(/^[a-zA-Z0-9_.-]+$/), password: z.string().min(8), }) type FormValues = z.infer export function RegisterForm({ onSuccess }: { onSuccess: () => void }) { const { t } = useTranslation() const { register: registerField, handleSubmit, formState: { errors, isSubmitting }, } = useForm({ resolver: zodResolver(schema) }) const onSubmit = async (values: FormValues) => { try { await registerUser(values.userName, values.password) const auth = await login(values.userName, values.password) applyAuthResponse(auth) onSuccess() } catch (error) { const message = error instanceof HttpError && error.status === 409 ? t('auth.duplicateUserName') : t('auth.genericError') toast.error(message) } } return (
{errors.userName ? (

{t('auth.userNameHint')}

) : (

{t('auth.userNameHint')}

)}
{errors.password ? (

{t('auth.passwordHint')}

) : (

{t('auth.passwordHint')}

)}
) }