import React, { useEffect } from 'react'; import { BrowserRouter, Routes, Route, useLocation, Navigate } from 'react-router-dom'; import { Sidebar } from './components/Sidebar'; import { TopBar } from './components/TopBar'; import { ErrorBoundary } from './components/ErrorBoundary'; import { Dashboard } from './pages/Dashboard'; import { LiveIncidents } from './pages/LiveIncidents'; import { FleetDispatch } from './pages/FleetDispatch'; import { PatientClinical } from './pages/PatientClinical'; import { HospitalsNetwork } from './pages/HospitalsNetwork'; import { AnalyticsReports } from './pages/AnalyticsReports'; import { UserManagement } from './pages/UserManagement'; import { PlatformConfig } from './pages/PlatformConfig'; import { AuditCompliance } from './pages/AuditCompliance'; import { SystemHealth } from './pages/SystemHealth'; import { HospitalConsole } from './pages/HospitalConsole'; import { MasterDataManagement } from './pages/MasterData'; import { CallerPortal } from './pages/CallerPortal'; import { Login } from './pages/Login'; import { FleetLogin } from './pages/FleetLogin'; import { FleetOperatorDashboard } from './pages/FleetOperatorDashboard'; import { PerspectiveLauncher } from './pages/PerspectiveLauncher'; import { RoleLogin } from './pages/RoleLogin'; import { HospitalLogin } from './pages/HospitalLogin'; import { ComingSoonPortal } from './pages/ComingSoonPortal'; import { Building2, Stethoscope, Activity, User, Scan, ShoppingCart } from 'lucide-react'; import { isTokenExpired, logout } from './utils/auth'; // --- ROLE-BASED ACCESS CONTROL --- const RoleProtectedRoute: React.FC<{ children: React.ReactNode, allowedRoles: string[], user: any }> = ({ children, allowedRoles, user }) => { const isAuthenticated = localStorage.getItem('teleems_auth') === 'true'; if (!isAuthenticated) return ; const userRoles = Array.isArray(user?.roles) ? user.roles.map((r: any) => String(r).toUpperCase().replace(/\s+/g, '_')) : []; const hasAccess = allowedRoles.some(role => userRoles.includes(role)) || userRoles.includes('CURESELECT_ADMIN'); if (!hasAccess) { console.log('[RBAC] Access Denied:', { required: allowedRoles, current: userRoles }); // Redirect to their respective "home" if they don't have access if (userRoles.includes('FLEET_OPERATOR')) return ; return ; } return <>{children}; }; function AppContent() { const location = useLocation(); // --- SESSION MONITORING --- // Periodically check if the token has expired useEffect(() => { const checkSession = () => { const token = localStorage.getItem('teleems_token'); const auth = localStorage.getItem('teleems_auth') === 'true'; if (auth && token && isTokenExpired(token)) { console.warn('Session expired. Logging out...'); logout(); } }; // Check on mount checkSession(); // Check on every route change checkSession(); // Periodically check every 30 seconds const interval = setInterval(checkSession, 30000); return () => clearInterval(interval); }, [location.pathname]); // --- DEVELOPMENT BYPASS --- // In a real production app, this would be removed. // For the user's request: "this admin so don't want login give me admin level access" /* Commented out to allow testing of launcher and login flow useEffect(() => { const isAuth = localStorage.getItem('teleems_auth') === 'true'; const isCaller = window.location.pathname === '/caller'; const isLogin = window.location.pathname === '/login'; if (!isAuth && !isCaller && !isLogin) { console.log('Dev Mode: Auto-authenticating as CureSelect Super Admin'); localStorage.setItem('teleems_auth', 'true'); localStorage.setItem('teleems_token', 'dev-super-token-2026'); localStorage.setItem('teleems_user', JSON.stringify({ id: 'admin-001', username: 'CureSelect Super Admin', roles: ['CURESELECT_ADMIN', 'ADMIN'], metadata: { organization: { company_name: 'CureSelect Healthcare LLP' } } })); // Force reload to update navigation window.location.reload(); } }, []); */ const isLoginPage = location.pathname.startsWith('/login') || location.pathname === '/fleet-login' || location.pathname === '/launcher'; const isAuthenticated = localStorage.getItem('teleems_auth') === 'true'; const user = JSON.parse(localStorage.getItem('teleems_user') || '{}'); // --- PUBLIC ROUTES (No Auth Required) --- if (isLoginPage || (location.pathname === '/' && !isAuthenticated)) { return ( } /> } /> } /> } /> } /> } /> } /> ); } // --- PROTECTED ROUTES (Auth Required) --- if (!isAuthenticated) { return ; } return (
: ) : ( ) } /> } /> } /> } /> } /> } /> } /> } /> } /> } /> } /> } /> } /> } /> {/* --- NEW PERSPECTIVE PORTALS --- */} } /> } /> } /> } /> } /> } /> } />
); } function App() { return ( ); } export default App;