import React, { useEffect } from 'react'; import { Navigate } from 'react-router-dom'; import useAuthStore from '../../../store/useAuthStore'; import { useAuthModal } from '../contexts/AuthModalContext'; interface AdminRouteProps { children: React.ReactNode; } /** * SECURITY NOTE: This component performs CLIENT-SIDE authorization checks only. * These checks are for UX purposes (showing/hiding UI elements). * * ALL authorization must be enforced server-side. Client-side checks can be bypassed * by modifying localStorage or browser DevTools. The backend API must validate * user roles and permissions for every request. */ const AdminRoute: React.FC = ({ children }) => { const { isAuthenticated, userInfo, isLoading } = useAuthStore(); const { openModal } = useAuthModal(); // SECURITY: Client-side role check - backend must also validate useEffect(() => { if (!isLoading && !isAuthenticated) { openModal('login'); } }, [isLoading, isAuthenticated, openModal]); if (isLoading) { return (

Authenticating...

); } if (!isAuthenticated) { return null; // Modal will be shown by AuthModalManager } // SECURITY: Client-side role check - MUST be validated server-side // This check can be bypassed by modifying localStorage const isAdmin = userInfo?.role === 'admin'; if (!isAdmin) { // Redirect to appropriate dashboard based on role if (userInfo?.role === 'staff') { return ; } else if (userInfo?.role === 'accountant') { return ; } return ; } return <>{children}; }; export default AdminRoute;