import { useState, type FormEvent } from 'react' import { useNavigate, useLocation } from 'react-router-dom' import { toast } from 'sonner' import { Button } from '@/components/ui/Button' import { Input } from '@/components/ui/Input' import { Label } from '@/components/ui/Label' import { useAuth } from '@/contexts/AuthContext' import axios from 'axios' export function LoginPage() { const { login } = useAuth() const navigate = useNavigate() const location = useLocation() const [email, setEmail] = useState('admin@solutionerp.local') const [password, setPassword] = useState('Admin@123456') const [isSubmitting, setIsSubmitting] = useState(false) async function handleSubmit(e: FormEvent) { e.preventDefault() setIsSubmitting(true) try { await login({ email, password }) const redirectTo = (location.state as { from?: { pathname: string } } | null)?.from?.pathname ?? '/inbox' navigate(redirectTo, { replace: true }) toast.success('Đăng nhập thành công') } catch (err) { let msg: string if (axios.isAxiosError(err)) { if (err.code === 'ERR_NETWORK' || err.message === 'Network Error') { const apiUrl = (import.meta.env.VITE_API_BASE_URL ?? window.location.origin) + '/api' msg = `Không thể kết nối ${apiUrl}. Check dev tools F12 → Network. Có thể do cache cũ — thử Ctrl+Shift+R hoặc tab ẩn danh.` } else { msg = err.response?.data?.detail ?? err.response?.data?.title ?? err.message } } else { msg = 'Lỗi không xác định' } toast.error(`Đăng nhập thất bại: ${msg}`, { duration: 8000 }) } finally { setIsSubmitting(false) } } return (
Solutions
ERP · Quản lý hợp đồng
Đăng nhập để tiếp tục
setEmail(e.target.value)} autoComplete="email" required />
setPassword(e.target.value)} autoComplete="current-password" required />
) }