16 lines
495 B
TypeScript
16 lines
495 B
TypeScript
import { Navigate, useLocation } from "react-router-dom"
|
|
|
|
export function RequireAuth({ children }: { children: React.ReactNode }) {
|
|
const isLoggedIn = sessionStorage.getItem("isLoggedIn") === "true"
|
|
const location = useLocation()
|
|
|
|
if (!isLoggedIn) {
|
|
// Redirect to login with the current path in the redirect parameter
|
|
return <Navigate
|
|
to={`/login?redirect=${encodeURIComponent(location.pathname + location.search)}`}
|
|
replace
|
|
/>
|
|
}
|
|
|
|
return <>{children}</>
|
|
}
|