proxy-go/web/app/dashboard/layout.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

67 lines
1.5 KiB
TypeScript

"use client"
import { useEffect } from "react"
import { useRouter } from "next/navigation"
import { Nav } from "@/components/nav"
export default function DashboardLayout({
children,
}: {
children: React.ReactNode
}) {
const router = useRouter()
useEffect(() => {
const token = localStorage.getItem("token")
if (!token) {
router.push("/login")
return
}
// 设置全局请求拦截器
const originalFetch = window.fetch
window.fetch = async (...args) => {
const [resource, config = {}] = args
const newConfig = {
...config,
headers: {
...(config.headers || {}),
'Authorization': `Bearer ${token}`,
},
}
try {
const response = await originalFetch(resource, newConfig)
if (response.status === 401) {
localStorage.removeItem("token")
router.push("/login")
return response
}
return response
} catch (error) {
console.error('请求失败:', error)
return Promise.reject(error)
}
}
// 验证 token 有效性
fetch("/api/check-auth").catch(() => {
localStorage.removeItem("token")
router.push("/login")
})
return () => {
window.fetch = originalFetch
}
}, [router])
return (
<div className="min-h-screen bg-gray-100">
<Nav />
<main className="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
{children}
</main>
</div>
)
}