import { useEffect } from 'react'; import { Navigate, useLocation, useNavigate } from 'react-router-dom'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { Gift, Loader2 } from 'lucide-react'; import { toast } from 'sonner'; import { loginSchema, type LoginInput } from '@family-wishlist/shared'; import { Button } from '@/components/ui/Button'; import { Input } from '@/components/ui/Input'; import { Label } from '@/components/ui/Label'; import { useAuthStore } from '@/features/auth/authStore'; import { ApiError } from '@/lib/api'; import { Footer } from '@/components/Layout/Footer'; import { LanguageSwitcher } from '@/components/LanguageSwitcher'; import { translateValidation, useI18n } from '@/i18n/i18n'; export function LoginPage() { const { t } = useI18n(); const { user, login } = useAuthStore(); const navigate = useNavigate(); const location = useLocation(); const from = (location.state as { from?: { pathname: string } } | null)?.from?.pathname ?? '/'; const { register, handleSubmit, formState: { errors, isSubmitting }, } = useForm({ resolver: zodResolver(loginSchema), defaultValues: { username: '', password: '' }, }); useEffect(() => { if (user) navigate(from, { replace: true }); }, [user, from, navigate]); if (user) return ; const submit = handleSubmit(async (values) => { try { await login(values.username, values.password); navigate(from, { replace: true }); } catch (err) { if (err instanceof ApiError) toast.error(err.message); else toast.error(t('login.failed')); } }); return (

{t('app.name')}

{t('login.title')}

{t('login.description')}

{errors.username && ( {translateValidation(t, errors.username.message)} )}
{errors.password && ( {translateValidation(t, errors.password.message)} )}
); }