105 lines
3.8 KiB
TypeScript
105 lines
3.8 KiB
TypeScript
"use client";
|
|
|
|
import { Suspense } from "react";
|
|
import { useRouter, useSearchParams } from "next/navigation";
|
|
import { FormEvent, useEffect, useState } from "react";
|
|
import { Loader2, LockKeyhole } from "lucide-react";
|
|
|
|
import { BrandMark } from "@/components/ui";
|
|
import { clearAuthRedirect, fetchJson } from "@/lib/api";
|
|
|
|
function LoginForm() {
|
|
const router = useRouter();
|
|
const searchParams = useSearchParams();
|
|
const next = searchParams.get("next") || "/";
|
|
const [username, setUsername] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
useEffect(() => {
|
|
void fetchJson("/api/auth/status")
|
|
.then((d) => {
|
|
if (d.authenticated) router.replace(next);
|
|
})
|
|
.catch(() => {});
|
|
}, [next, router]);
|
|
|
|
async function onSubmit(e: FormEvent) {
|
|
e.preventDefault();
|
|
setBusy(true);
|
|
setError("");
|
|
try {
|
|
await fetchJson("/api/auth/login", {
|
|
method: "POST",
|
|
body: JSON.stringify({ username, password }),
|
|
});
|
|
clearAuthRedirect();
|
|
router.replace(next);
|
|
router.refresh();
|
|
} catch (err) {
|
|
setError((err as Error).message || "登录失败");
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="relative flex min-h-screen items-center justify-center px-4">
|
|
<div className="ambient-orb a" />
|
|
<div className="ambient-orb b" />
|
|
<div className="relative z-10 w-full max-w-md animate-fade-up rounded-2xl border border-white/10 bg-black/40 p-8 shadow-2xl backdrop-blur-md">
|
|
<div className="mb-6 flex flex-col items-center text-center">
|
|
<BrandMark className="mb-4 h-14 w-14" />
|
|
<p className="font-display text-xs uppercase tracking-[0.28em] text-teal-300/90">Live Radar</p>
|
|
<h1 className="mt-2 font-display text-2xl font-bold text-white">登录监控中心</h1>
|
|
<p className="mt-2 text-sm text-slate-500">抖音 + YouTube 直播监控 · 请登录后继续</p>
|
|
</div>
|
|
|
|
<form onSubmit={(e) => void onSubmit(e)} className="space-y-4">
|
|
<label className="block text-xs text-slate-400">
|
|
用户名
|
|
<input
|
|
type="text"
|
|
autoComplete="username"
|
|
value={username}
|
|
onChange={(e) => setUsername(e.target.value)}
|
|
placeholder="用户名"
|
|
className="mt-1 w-full rounded-xl border border-white/10 bg-slate-950 px-3 py-2.5 text-sm text-white outline-none ring-teal-400/30 focus:ring-2"
|
|
required
|
|
/>
|
|
</label>
|
|
<label className="block text-xs text-slate-400">
|
|
密码
|
|
<input
|
|
type="password"
|
|
autoComplete="current-password"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
className="mt-1 w-full rounded-xl border border-white/10 bg-slate-950 px-3 py-2.5 text-sm text-white outline-none ring-teal-400/30 focus:ring-2"
|
|
required
|
|
/>
|
|
</label>
|
|
{error ? <div className="text-sm text-rose-300">{error}</div> : null}
|
|
<button
|
|
type="submit"
|
|
disabled={busy}
|
|
className="inline-flex w-full items-center justify-center gap-2 rounded-xl bg-[#ff2d55] px-4 py-2.5 text-sm font-medium text-white hover:bg-[#ff4d6d] disabled:opacity-60"
|
|
>
|
|
{busy ? <Loader2 className="h-4 w-4 animate-spin" /> : <LockKeyhole className="h-4 w-4" />}
|
|
登录
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default function LoginPage() {
|
|
return (
|
|
<Suspense fallback={<div className="flex min-h-screen items-center justify-center text-slate-500">加载中…</div>}>
|
|
<LoginForm />
|
|
</Suspense>
|
|
);
|
|
}
|