"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { login, AuthError } from "@duoc-thu/api-client"; export default function AdminLoginPage() { const router = useRouter(); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(null); const [pending, setPending] = useState(false); async function onSubmit(event: React.FormEvent) { event.preventDefault(); setError(null); setPending(true); try { const user = await login({ username, password }); if (user.role !== "admin") { setError("Tài khoản này không có quyền quản trị."); return; } router.push("/admin"); router.refresh(); } catch (err) { setError( err instanceof AuthError && err.status === 401 ? "Sai tên đăng nhập hoặc mật khẩu." : "Không thể đăng nhập lúc này. Vui lòng thử lại." ); } finally { setPending(false); } } return (

Đăng nhập quản trị

setUsername(e.target.value)} autoComplete="username" required />
setPassword(e.target.value)} autoComplete="current-password" required />
{error &&

{error}

}
); }