import { useEffect } from 'react';
import { Navigate, Outlet, useLocation } from 'react-router-dom';
import { useAuth } from '@/lib/AuthContext';
import UserNotRegisteredError from '@/components/UserNotRegisteredError';
const DefaultFallback = () => (
);
/**
* `unauthenticatedElement` defaults to the login page.
*
* It used to default to `undefined`, which renders nothing. That was invisible
* while the data client reported the seeded user as permanently signed in and
* this branch was unreachable; now that `GET /me` can genuinely answer 401, the
* default is what a signed-out visitor actually sees, and a blank screen is not
* an acceptable answer to "you are not signed in".
*
* The attempted path travels in location state so signing in returns the
* visitor to where they were going.
*/
interface ProtectedRouteProps {
fallback?: any;
unauthenticatedElement?: any;
}
export default function ProtectedRoute({ fallback = , unauthenticatedElement }: ProtectedRouteProps) {
const location = useLocation();
const { isAuthenticated, isLoadingAuth, authChecked, authError, checkUserAuth } = useAuth();
const signedOut = unauthenticatedElement ?? (
);
useEffect(() => {
if (!authChecked && !isLoadingAuth) {
checkUserAuth();
}
}, [authChecked, isLoadingAuth, checkUserAuth]);
if (isLoadingAuth || !authChecked) {
return fallback;
}
if (authError) {
if (authError.type === 'user_not_registered') {
return ;
}
return signedOut;
}
if (!isAuthenticated) {
return signedOut;
}
return ;
}