Files
juip-web/src/components/authGuard.tsx

22 lines
586 B
TypeScript
Raw Normal View History

2026-07-30 13:55:28 +08:00
import { Navigate, useLocation } from "react-router-dom"
2026-09-04 13:21:47 +08:00
import { useProfileStore } from "@/components/stores/profile"
2026-07-30 13:55:28 +08:00
export default function AuthGuard({ children }: { children: React.ReactNode }) {
const location = useLocation()
const token = useProfileStore(state => state.token)
const userInfo = useProfileStore(state => state.userInfo)
const isValid = token !== null && userInfo !== null
2026-07-30 13:55:28 +08:00
2026-08-07 16:54:21 +08:00
if (!isValid) {
return (
<Navigate
to={`/login?redirect=${encodeURIComponent(location.pathname)}`}
replace
/>
)
}
2026-07-30 13:55:28 +08:00
return <>{children}</>
}