feat: add auth guards to protected routes and extend requireAuth coverage

This commit is contained in:
zetaloop
2026-02-20 22:38:29 +08:00
parent c0896faa78
commit 4cc4383603
6 changed files with 63 additions and 4 deletions
+32
View File
@@ -0,0 +1,32 @@
"use client"
import { useEffect } from "react"
import { Button } from "@/components/ui/button"
import { useAuthStore } from "@/store/auth"
import { useLoginDialogStore } from "@/store/login-dialog"
interface AuthGuardProps {
children: React.ReactNode
}
export function AuthGuard({ children }: AuthGuardProps) {
const isAuthenticated = useAuthStore((s) => s.isAuthenticated)
const openLoginDialog = useLoginDialogStore((s) => s.openLoginDialog)
useEffect(() => {
if (!isAuthenticated) {
openLoginDialog()
}
}, [isAuthenticated, openLoginDialog])
if (isAuthenticated) {
return <>{children}</>
}
return (
<div className="flex min-h-[50vh] flex-col items-center justify-center gap-4 text-center">
<p className="text-sm text-muted-foreground">访</p>
<Button onClick={openLoginDialog}></Button>
</div>
)
}