proxy-go/web/app/login/page.tsx
wood chen 33d6a51416 refactor(web): Migrate to modern web frontend and simplify admin routes
- Remove legacy static files, templates, and JavaScript
- Update main.go to serve SPA-style web application
- Modify admin route handling to support client-side routing
- Simplify configuration and metrics API endpoints
- Remove server-side template rendering in favor of static file serving
- Update Dockerfile and GitHub Actions to build web frontend
2025-02-15 11:44:09 +08:00

89 lines
2.6 KiB
TypeScript

"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("/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("/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 (
<div className="min-h-screen flex items-center justify-center bg-gray-100">
<Card className="w-[400px]">
<CardHeader>
<CardTitle className="text-2xl text-center"></CardTitle>
</CardHeader>
<CardContent>
<form onSubmit={handleLogin} className="space-y-4">
<div className="space-y-2">
<label htmlFor="password" className="text-sm font-medium">
</label>
<Input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="请输入管理密码"
required
/>
</div>
<Button type="submit" className="w-full" disabled={loading}>
{loading ? "登录中..." : "登录"}
</Button>
</form>
</CardContent>
</Card>
</div>
)
}