Files
acmcc/src/components/RequireAdmin.tsx
T
admin 8ac0edfbd9 Admin-only Accounting tab + platform COA consolidation
Phase 1: gate the Accounting sidebar item and /dashboard/accounting route
behind isAdmin via a RequireAdmin guard; Financial Reports stay visible.

Phase 2: platform associations now read the Chart of Accounts from
accounting.accounts (single source) instead of public.chart_of_accounts.
Shared fetchChartOfAccounts() normalizes both sources; central COA hooks
and ChartOfAccountsDropdown route through it (reads only, no migration).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-01 21:21:48 -04:00

24 lines
760 B
TypeScript

import { Navigate } from "react-router-dom";
import { useAuth } from "@/contexts/AuthContext";
/**
* Route guard for admin-only areas (e.g. the Accounting platform).
* Shows a spinner while auth resolves, then redirects non-admins to the
* dashboard. Admin status respects "view as" (uses effective `isAdmin`).
*/
export function RequireAdmin({ children }: { children: React.ReactNode }) {
const { loading, isAdmin } = useAuth();
if (loading) {
return (
<div className="flex min-h-[40vh] items-center justify-center">
<div className="h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent" />
</div>
);
}
if (!isAdmin) return <Navigate to="/dashboard" replace />;
return <>{children}</>;
}