mirror of
https://github.com/ramvignesh-b/pi-ku.git
synced 2026-05-04 19:10:52 +00:00
39 lines
1.1 KiB
TypeScript
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}</>;
|
|
}
|