diff --git a/App.tsx b/App.tsx index 929dd5a..3d299d9 100644 --- a/App.tsx +++ b/App.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState, useEffect } from 'react'; import { HashRouter as Router, Routes, Route, Navigate, useLocation } from 'react-router-dom'; import { Layout } from './components/Layout'; import { Dashboard } from './pages/Dashboard'; @@ -8,34 +8,62 @@ import { SuperAdmin } from './pages/SuperAdmin'; import { TeamManagement } from './pages/TeamManagement'; import { Login } from './pages/Login'; import { UserProfile } from './pages/UserProfile'; +import { getUserById } from './services/dataService'; +import { User } from './types'; -const AppLayout: React.FC<{ children: React.ReactNode }> = ({ children }) => { +const AuthGuard: React.FC<{ children: React.ReactNode }> = ({ children }) => { + const [user, setUser] = useState(null); + const [loading, setLoading] = useState(true); const location = useLocation(); - const isLoginPage = location.pathname === '/login'; - if (isLoginPage) { - return <>{children}; + useEffect(() => { + const checkAuth = async () => { + const storedUserId = localStorage.getItem('ctms_user_id'); + if (!storedUserId) { + setLoading(false); + return; + } + + try { + const fetchedUser = await getUserById(storedUserId); + if (fetchedUser) { + setUser(fetchedUser); + } else { + localStorage.removeItem('ctms_user_id'); + } + } catch (err) { + console.error("Auth check failed", err); + } finally { + setLoading(false); + } + }; + checkAuth(); + }, [location.pathname]); + + if (loading) { + return
Carregando...
; } - return {children}; + if (!user) { + return ; + } + + return {children}; }; const App: React.FC = () => { return ( - - - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - - + + } /> + } /> + } /> + } /> + } /> + } /> + } /> + } /> + ); }; diff --git a/components/Layout.tsx b/components/Layout.tsx index 17c0028..4cbd84c 100644 --- a/components/Layout.tsx +++ b/components/Layout.tsx @@ -20,29 +20,10 @@ const SidebarItem = ({ to, icon: Icon, label, collapsed }: { to: string, icon: a ); -export const Layout: React.FC<{ children: React.ReactNode }> = ({ children }) => { +export const Layout: React.FC<{ children: React.ReactNode, currentUser: User }> = ({ children, currentUser }) => { const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const location = useLocation(); const navigate = useNavigate(); - const [currentUser, setCurrentUser] = useState(null); - - useEffect(() => { - const fetchUser = async () => { - const storedUserId = localStorage.getItem('ctms_user_id'); - if (!storedUserId) { - navigate('/login'); - return; - } - - const user = await getUserById(storedUserId); - if (user) { - setCurrentUser(user); - } else { - navigate('/login'); - } - }; - fetchUser(); - }, [navigate]); const handleLogout = () => { localStorage.removeItem('ctms_user_id'); @@ -50,8 +31,6 @@ export const Layout: React.FC<{ children: React.ReactNode }> = ({ children }) => navigate('/login'); }; - if (!currentUser) return null; // Brief null return while navigating or fetching - const isSuperAdmin = currentUser.role === 'super_admin'; return (