Files
inventory/inventory/src/components/auth/RequireAuth.tsx

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}</>
}