All files / web/src/routes __root.tsx

89.74% Statements 35/39
80% Branches 8/10
100% Functions 2/2
89.74% Lines 35/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561x           1x 1x   31x 31x 31x   31x 31x 5x 5x   26x 26x   31x       31x 1x 1x 31x 1x       31x   1x 1x 1x 1x   27x 27x 27x 27x 27x 27x 27x 27x 27x     27x 27x   27x  
import {
  createRootRoute,
  isRedirect,
  Outlet,
  redirect,
} from '@tanstack/react-router';
import { getAuthStatus } from '@web/api/v1/super-agents/auth';
import { Suspense } from 'react';
 
async function checkAuth({
  location,
}: {
  location: { pathname: string };
}): Promise<void> {
  if (location.pathname === '/login') {
    return;
  }
 
  try {
    const data = await getAuthStatus();
 
    if (!data) {
      throw redirect({ to: '/login' });
    }
 
    if (data.authRequired && !data.authenticated) {
      throw redirect({ to: '/login' });
    }
  } catch (e) {
    if (isRedirect(e)) throw e;
    // Network error — default to login for security
    throw redirect({ to: '/login' });
  }
}
 
export const Route = createRootRoute({
  beforeLoad: checkAuth,
  component: RootComponent,
});
 
function RootComponent() {
  return (
    <Suspense
      fallback={
        <div className="flex items-center justify-center h-screen">
          <div className="text-center">
            <p className="text-lg">Loading...</p>
          </div>
        </div>
      }
    >
      <Outlet />
    </Suspense>
  );
}