"use client"; import { startTransition, useMemo, useState, type FormEvent } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import type { UserRole } from "@/lib/types"; interface LoginResponse { ok?: boolean; error?: string; user?: { role: UserRole; }; } function getErrorMessage(error: unknown, fallbackMessage: string): string { return error instanceof Error ? error.message : fallbackMessage; } export default function LoginForm() { const router = useRouter(); const searchParams = useSearchParams(); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [errorMessage, setErrorMessage] = useState(""); const [isPending, setIsPending] = useState(false); const notice = useMemo(() => { const reason = searchParams.get("reason"); if (reason === "inactive") { return "当前账号不可用,请联系管理员。"; } return "使用管理员或已创建的普通用户登录。"; }, [searchParams]); async function handleSubmit( event: FormEvent, ): Promise { event.preventDefault(); setErrorMessage(""); setIsPending(true); try { const response = await fetch("/api/auth/login", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ username, password, }), }); const payload = (await response.json()) as LoginResponse; if (!response.ok || payload.ok !== true || !payload.user) { throw new Error(payload.error || "登录失败,请稍后重试。"); } const currentUser = payload.user; startTransition(() => { router.push(currentUser.role === "admin" ? "/admin" : "/studio"); router.refresh(); }); } catch (error) { setErrorMessage(getErrorMessage(error, "登录失败,请稍后重试。")); } finally { setIsPending(false); } } return (

Secure Access

登录后才能使用图片生成与后台管理。

{notice}

{errorMessage ?
{errorMessage}
: null}
); }