v4.5 Demo initial commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
"use client"
|
||||
|
||||
import { useEffect, ReactNode } from "react"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { useAuth } from "@/lib/auth-context"
|
||||
|
||||
interface ProtectedRouteProps {
|
||||
children: ReactNode | ((props: { user: any; logout: () => void }) => ReactNode);
|
||||
}
|
||||
|
||||
export function ProtectedRoute({ children }: ProtectedRouteProps) {
|
||||
const { user, logout } = useAuth()
|
||||
const router = useRouter()
|
||||
|
||||
useEffect(() => {
|
||||
if (!user) {
|
||||
router.push("/login")
|
||||
}
|
||||
}, [user, router])
|
||||
|
||||
if (!user) {
|
||||
return null
|
||||
}
|
||||
|
||||
return typeof children === "function"
|
||||
? children({ user, logout })
|
||||
: children
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user