2026-04-05 13:11:44 +02:00
|
|
|
import { useRef } from 'react';
|
2026-04-09 21:52:34 +02:00
|
|
|
import { Navigate, Outlet } from 'react-router';
|
2026-04-05 01:17:47 +02:00
|
|
|
import { useLogto } from '@logto/react';
|
|
|
|
|
import { Spinner } from '@cameleer/design-system';
|
2026-04-04 21:48:56 +02:00
|
|
|
|
2026-04-09 21:52:34 +02:00
|
|
|
export function ProtectedRoute({ children }: { children?: React.ReactNode }) {
|
2026-04-05 01:17:47 +02:00
|
|
|
const { isAuthenticated, isLoading } = useLogto();
|
2026-04-05 13:11:44 +02:00
|
|
|
// The Logto SDK sets isLoading=true for EVERY async method (getAccessToken, etc.),
|
|
|
|
|
// not just initial auth. Only gate on the initial load — once isLoading is false
|
|
|
|
|
// for the first time, never show the spinner again.
|
|
|
|
|
const initialLoadDone = useRef(false);
|
2026-04-05 01:17:47 +02:00
|
|
|
|
2026-04-05 13:11:44 +02:00
|
|
|
if (!isLoading) {
|
|
|
|
|
initialLoadDone.current = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!initialLoadDone.current) {
|
2026-04-05 01:17:47 +02:00
|
|
|
return (
|
|
|
|
|
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: '100vh' }}>
|
|
|
|
|
<Spinner />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 21:48:56 +02:00
|
|
|
if (!isAuthenticated) return <Navigate to="/login" replace />;
|
2026-04-09 21:52:34 +02:00
|
|
|
return children ? <>{children}</> : <Outlet />;
|
2026-04-04 21:48:56 +02:00
|
|
|
}
|