All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m52s
- Restricted Agent view to own dashboard and hid management tabs. - Allowed Managers to create teams and members but restricted them from editing roles or emails. - Allowed Admins to update their own email via profile. - Protected Admin roles from being modified by anyone other than Super Admins.
90 lines
3.4 KiB
TypeScript
90 lines
3.4 KiB
TypeScript
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';
|
|
import { UserDetail } from './pages/UserDetail';
|
|
import { AttendanceDetail } from './pages/AttendanceDetail';
|
|
import { SuperAdmin } from './pages/SuperAdmin';
|
|
import { TeamManagement } from './pages/TeamManagement';
|
|
import { Teams } from './pages/Teams';
|
|
import { Login } from './pages/Login';
|
|
import { ForgotPassword } from './pages/ForgotPassword';
|
|
import { ResetPassword } from './pages/ResetPassword';
|
|
import { SetupAccount } from './pages/SetupAccount';
|
|
import { UserProfile } from './pages/UserProfile';
|
|
import { getUserById, logout } from './services/dataService';
|
|
import { User } from './types';
|
|
|
|
const AuthGuard: React.FC<{ children: React.ReactNode, roles?: string[] }> = ({ children, roles }) => {
|
|
const [user, setUser] = useState<User | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
const location = useLocation();
|
|
|
|
useEffect(() => {
|
|
const checkAuth = async () => {
|
|
const storedUserId = localStorage.getItem('ctms_user_id');
|
|
const storedToken = localStorage.getItem('ctms_token');
|
|
|
|
if (!storedUserId || !storedToken || storedToken === 'undefined' || storedToken === 'null') {
|
|
if (storedToken) logout(); // Limpar se for "undefined" string
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const fetchedUser = await getUserById(storedUserId);
|
|
if (fetchedUser && fetchedUser.status === 'active') {
|
|
setUser(fetchedUser);
|
|
} else {
|
|
logout();
|
|
setUser(null);
|
|
}
|
|
} catch (err) {
|
|
console.error("Auth check failed", err);
|
|
logout();
|
|
setUser(null);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
checkAuth();
|
|
}, [location.pathname]);
|
|
|
|
if (loading) {
|
|
return <div className="flex h-screen items-center justify-center bg-slate-50 text-slate-400">Carregando...</div>;
|
|
}
|
|
|
|
if (!user) {
|
|
return <Navigate to="/login" replace />;
|
|
}
|
|
|
|
if (roles && !roles.includes(user.role)) {
|
|
return <Navigate to="/" replace />;
|
|
}
|
|
|
|
return <Layout>{children}</Layout>;
|
|
};
|
|
|
|
const App: React.FC = () => {
|
|
return (
|
|
<Router>
|
|
<Routes>
|
|
<Route path="/login" element={<Login />} />
|
|
<Route path="/forgot-password" element={<ForgotPassword />} />
|
|
<Route path="/reset-password" element={<ResetPassword />} />
|
|
<Route path="/setup-account" element={<SetupAccount />} />
|
|
<Route path="/" element={<AuthGuard><Dashboard /></AuthGuard>} />
|
|
<Route path="/admin/users" element={<AuthGuard roles={['super_admin', 'admin', 'manager']}><TeamManagement /></AuthGuard>} />
|
|
<Route path="/admin/teams" element={<AuthGuard roles={['super_admin', 'admin', 'manager']}><Teams /></AuthGuard>} />
|
|
<Route path="/users/:id" element={<AuthGuard><UserDetail /></AuthGuard>} />
|
|
<Route path="/attendances/:id" element={<AuthGuard><AttendanceDetail /></AuthGuard>} />
|
|
<Route path="/super-admin" element={<AuthGuard roles={['super_admin']}><SuperAdmin /></AuthGuard>} />
|
|
<Route path="/profile" element={<AuthGuard><UserProfile /></AuthGuard>} />
|
|
<Route path="*" element={<Navigate to="/" replace />} />
|
|
</Routes>
|
|
</Router>
|
|
);
|
|
};
|
|
|
|
export default App;
|