Files
cameleer-saas/ui/src/auth/ProtectedRoute.tsx
hsiegeln bf3aa57274 feat: restructure frontend routes — vendor/tenant persona split
Splits the flat 3-page UI into /vendor/* (platform:admin) and /tenant/*
(all authenticated users) route trees, with stub pages, new API hooks,
updated Layout with persona-aware sidebar, and SpaController forwarding.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-09 22:29:59 +02:00

28 lines
953 B
TypeScript

import { useRef } from 'react';
import { Navigate, Outlet } from 'react-router';
import { useLogto } from '@logto/react';
import { Spinner } from '@cameleer/design-system';
export function ProtectedRoute({ children }: { children?: React.ReactNode }) {
const { isAuthenticated, isLoading } = useLogto();
// 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);
if (!isLoading) {
initialLoadDone.current = true;
}
if (!initialLoadDone.current) {
return (
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: '100vh' }}>
<Spinner />
</div>
);
}
if (!isAuthenticated) return <Navigate to="/login" replace />;
return children ? <>{children}</> : <Outlet />;
}