Files
pi-ku/frontend/src/components/RouteGuards.tsx
T
2026-04-29 23:12:05 +05:30

39 lines
1.1 KiB
TypeScript

import { Navigate, useLocation } from "react-router-dom";
import { ROUTES } from "../config/routes";
import { useAuth } from "../hooks/useAuth";
import SplashScreen from "./SplashScreen";
/**
* Private route guard.
* If not authenticated, capture the current url in route
* state so the Login component can link them back after sign-in
*/
export function ProtectedRoute({ children }: { children: React.ReactNode }) {
const { isAuthenticated, isInitializing } = useAuth();
const location = useLocation();
if (isInitializing) return <SplashScreen />;
if (!isAuthenticated) {
return <Navigate to={ROUTES.LOGIN} state={{ from: location }} replace />;
}
return <>{children}</>;
}
/**
* Public - auth route guard.
* If authenticated, redirect all the auth related flows to the drawer
*/
export function PublicRoute({ children }: { children: React.ReactNode }) {
const { isAuthenticated, isInitializing } = useAuth();
if (isInitializing) return <SplashScreen />;
if (isAuthenticated) {
return <Navigate to={ROUTES.DRAWER} replace />;
}
return <>{children}</>;
}