"use client" import { useState } from "react" import { useRouter } from "next/navigation" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { useToast } from "@/components/ui/use-toast" export default function LoginPage() { const [password, setPassword] = useState("") const [loading, setLoading] = useState(false) const router = useRouter() const { toast } = useToast() const handleLogin = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) try { const response = await fetch("/admin/api/auth", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", }, body: `password=${encodeURIComponent(password)}`, }) if (!response.ok) { throw new Error("登录失败") } const data = await response.json() localStorage.setItem("token", data.token) // 验证token const verifyResponse = await fetch("/admin/api/check-auth", { headers: { 'Authorization': `Bearer ${data.token}`, }, }); if (verifyResponse.ok) { // 登录成功,跳转到仪表盘 router.push("/dashboard") } else { throw new Error("Token验证失败") } } catch (error) { toast({ title: "错误", description: error instanceof Error ? error.message : "登录失败", variant: "destructive", }) } finally { setLoading(false) } } return (