ajout isadmin check

This commit is contained in:
Eddi3_As 2025-04-02 20:40:36 -04:00
parent 0ae9791e5b
commit 10169b93f1
2 changed files with 24 additions and 3 deletions

View file

@ -34,7 +34,7 @@ import Stats from './pages/Admin/Stats';
const App: React.FC = () => { const App: React.FC = () => {
const [isAuthenticated, setIsAuthenticated] = useState(ApiService.isLoggedIn()); const [isAuthenticated, setIsAuthenticated] = useState(ApiService.isLoggedIn());
const [isTeacherAuthenticated, setIsTeacherAuthenticated] = useState(ApiService.isLoggedInTeacher()); const [isTeacherAuthenticated, setIsTeacherAuthenticated] = useState(ApiService.isLoggedInTeacher());
//const [isAdmin, setIsAdmin] = useState(false); const [isAdmin, setIsAdmin] = useState(false);
const [isRoomRequireAuthentication, setRoomsRequireAuth] = useState(null); const [isRoomRequireAuthentication, setRoomsRequireAuth] = useState(null);
const location = useLocation(); const location = useLocation();
@ -43,7 +43,7 @@ const App: React.FC = () => {
const checkLoginStatus = () => { const checkLoginStatus = () => {
setIsAuthenticated(ApiService.isLoggedIn()); setIsAuthenticated(ApiService.isLoggedIn());
setIsTeacherAuthenticated(ApiService.isLoggedInTeacher()); setIsTeacherAuthenticated(ApiService.isLoggedInTeacher());
//setIsAdmin(ApiService.isAdmin()); setIsAdmin(ApiService.isAdmin());
}; };
const fetchAuthenticatedRooms = async () => { const fetchAuthenticatedRooms = async () => {
@ -63,7 +63,7 @@ const App: React.FC = () => {
return ( return (
<div className="content"> <div className="content">
<Header isLoggedIn={isAuthenticated} isAdmin={true} handleLogout={handleLogout} /> <Header isLoggedIn={isAuthenticated} isAdmin={isAdmin} handleLogout={handleLogout} />
<div className="app"> <div className="app">
<main> <main>
<Routes> <Routes>

View file

@ -120,6 +120,27 @@ class ApiService {
} }
} }
public isAdmin(): boolean {
let isAdmin = false;
const token = this.getToken();
if (token == null) {
return isAdmin;
}
try {
const jsonObj = jwtDecode(token) as { roles: string[] };
if (jsonObj.roles.includes('admin')) {
isAdmin = true;
}
return isAdmin;
} catch (error) {
console.error("Error decoding token:", error);
return isAdmin;
}
}
public saveUsername(username: string): void { public saveUsername(username: string): void {
if (!username || username.length === 0) { if (!username || username.length === 0) {
return; return;