ProtectedRoute.tsx
457 bytes
| 1 | import { Navigate, Outlet, useLocation } from "react-router-dom"; |
|---|---|
| 2 | import { useAuth } from "../lib/auth"; |
| 3 | import { LoadingScreen } from "./Ui"; |
| 4 | |
| 5 | export function ProtectedRoute() { |
| 6 | const { user, loading } = useAuth(); |
| 7 | const location = useLocation(); |
| 8 | |
| 9 | if (loading) { |
| 10 | return <LoadingScreen />; |
| 11 | } |
| 12 | if (!user) { |
| 13 | return <Navigate to={`/login?next=${encodeURIComponent(location.pathname + location.search)}`} replace />; |
| 14 | } |
| 15 | return <Outlet />; |
| 16 | } |
| 17 | |