import React, { useState } from 'react'; import { Link, Navigate, useNavigate, useLocation } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { useAuth } from '../context/AuthContext'; import { useSite } from '../context/SiteContext'; import toast from 'react-hot-toast'; export default function Login() { const { t } = useTranslation(); const { login, user } = useAuth(); const { site } = useSite(); const navigate = useNavigate(); const location = useLocation(); const [form, setForm] = useState({ username: '', password: '' }); const [loading, setLoading] = useState(false); // If already logged in, redirect via component (not navigate in render) if (user) { const from = location.state?.from?.pathname || '/dashboard'; return ; } const handleSubmit = async (e) => { e.preventDefault(); if (!form.username || !form.password) { toast.error(t('login.fillAllFields')); return; } setLoading(true); try { const result = await login(form.username, form.password); if (result.success) { toast.success(t('login.welcomeBackToast')); const from = location.state?.from?.pathname || '/dashboard'; navigate(from, { replace: true }); return; // component may unmount — skip setLoading } // error toast is handled by api interceptor for success:false } catch (err) { // Network error handled by interceptor } setLoading(false); }; return (

{t('login.welcomeBack')}

{site?.name ? t('login.signInTo', { name: site.name }) : t('login.signInToDefault')}

setForm({ ...form, username: e.target.value })} className="input" placeholder={t('login.enterUsername')} autoComplete="username" required />
setForm({ ...form, password: e.target.value })} className="input" placeholder={t('login.enterPassword')} autoComplete="current-password" required />

{t('login.noAccount')}{' '} {t('login.createOne')}

); }