Files
solo-company-feed/app/login/login-form.tsx
爱喝水的木子 bfdf4843e1 OPC
2026-03-13 16:28:51 +08:00

60 lines
1.9 KiB
TypeScript

"use client";
import { useState } from "react";
import { useRouter, useSearchParams } from "next/navigation";
export default function LoginForm() {
const [password, setPassword] = useState("");
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const router = useRouter();
const params = useSearchParams();
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
setLoading(true);
setError("");
try {
const res = await fetch("/api/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ password })
});
if (!res.ok) {
const data = await res.json().catch(() => ({}));
setError(data.error || "登录失败");
} else {
const next = params.get("next") || "/admin";
router.push(next);
router.refresh();
}
} finally {
setLoading(false);
}
}
return (
<form onSubmit={handleSubmit} className="space-y-4">
<label className="block space-y-1">
<span className="text-sm font-medium text-slate-700"></span>
<input
type="password"
required
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full rounded-xl border border-slate-200 bg-white px-3 py-2 text-sm shadow-inner focus:border-brand-500 focus:outline-none"
placeholder="输入 ADMIN_PASS"
/>
</label>
{error ? <p className="text-sm text-red-500">{error}</p> : null}
<button
type="submit"
disabled={loading}
className="w-full rounded-full bg-brand-600 px-4 py-2 text-sm font-medium text-white shadow hover:bg-brand-700 disabled:cursor-not-allowed disabled:opacity-60"
>
{loading ? "登录中…" : "登录"}
</button>
</form>
);
}