"use client"; import { useEffect, useMemo, useRef, useState } from "react"; import type { FormEvent } from "react"; import Image from "next/image"; import { Loader2, LogIn, RefreshCw } from "lucide-react"; import { pulseFeedback, revealChildren, runScopedMotion } from "@/lib/ui/motion"; export function AuthLoginPanel({ next, configured, message, missing }: { next: string; configured: boolean; message?: string | null; missing?: string[]; }) { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [code, setCode] = useState(""); const [randomStr, setRandomStr] = useState(""); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const screenRef = useRef(null); const feedbackRef = useRef(null); const hasMissingConfig = !configured && Boolean(missing?.length); useEffect(() => { return runScopedMotion(screenRef, (scope) => revealChildren(scope)); }, []); useEffect(() => { pulseFeedback(feedbackRef.current); }, [error, message, missing?.join(",")]); const captchaSrc = useMemo(() => { if (!randomStr) return ""; return `/api/auth/captcha?randomStr=${encodeURIComponent(randomStr)}`; }, [randomStr]); function refreshCaptcha() { setCode(""); setRandomStr(crypto.randomUUID()); } async function submit(event: FormEvent) { event.preventDefault(); if (!configured || submitting) return; setSubmitting(true); setError(null); try { const payload: Record = { username, password, next }; if (code.trim() && randomStr) { payload.code = code.trim(); payload.randomStr = randomStr; } const response = await fetch("/api/auth/password", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload) }); const result = await response.json().catch(() => ({})) as { error?: string; redirectTo?: string }; if (!response.ok) throw new Error(result.error || "登录失败"); window.location.assign(result.redirectTo || next || "/create"); } catch (err) { setError(err instanceof Error ? err.message : String(err)); refreshCaptcha(); } finally { setSubmitting(false); } } return (

智念AIGC平台

账户登录

{message || hasMissingConfig || error ? (
{message ?
{message}
: null} {hasMissingConfig ? (
缺少配置:{missing?.join("、")}
) : null} {error ?
{error}
: null}
) : null}
); }