import React, { useState, useEffect } from 'react'; import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'; import { Navbar } from './components/Navbar'; import { HomePage } from './pages/Home'; import { LoginPage, RegisterPage } from './pages/Auth'; import { DashboardPage } from './pages/Dashboard'; import { PlayersPage } from './pages/Players'; import { ProfilePage } from './pages/Profile'; import { FinancePage } from './pages/Finance'; import { AdminPage } from './pages/Admin'; import { MatchesPage } from './pages/Matches'; function App() { // Initialize auth state synchronously from localStorage const initializeAuth = () => { const token = localStorage.getItem('token'); const userData = localStorage.getItem('user'); if (token && userData) { try { return { isAuthenticated: true, user: JSON.parse(userData) }; } catch { return { isAuthenticated: false, user: null }; } } return { isAuthenticated: false, user: null }; }; const initialAuth = initializeAuth(); const [isAuthenticated, setIsAuthenticated] = useState(initialAuth.isAuthenticated); const [user, setUser] = useState(initialAuth.user); const [isLoading, setIsLoading] = useState(false); const isAdmin = user?.role === 'admin'; const updateAuthState = () => { // Check if user is logged in const token = localStorage.getItem('token'); const userData = localStorage.getItem('user'); if (token && userData) { try { setIsAuthenticated(true); setUser(JSON.parse(userData)); } catch { setIsAuthenticated(false); setUser(null); } } else { setIsAuthenticated(false); setUser(null); } }; useEffect(() => { // Listen for auth changes (login, logout) const handleAuthChange = () => { updateAuthState(); }; window.addEventListener('authchange', handleAuthChange); window.addEventListener('storage', handleAuthChange); return () => { window.removeEventListener('authchange', handleAuthChange); window.removeEventListener('storage', handleAuthChange); }; }, []); if (isLoading) { return
Loading...
; } return ( <> {/* Public Routes */} } /> : } /> : } /> {/* Protected Routes */} {isAuthenticated ? ( <> } /> } /> } /> } /> } /> } /> : } /> ) : ( <> } /> } /> } /> } /> } /> } /> )} ); } export default App;