diff --git a/client/src/App.tsx b/client/src/App.tsx index fb20f4f..6d0fae2 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -34,7 +34,7 @@ import Stats from './pages/Admin/Stats'; const App: React.FC = () => { const [isAuthenticated, setIsAuthenticated] = useState(ApiService.isLoggedIn()); const [isTeacherAuthenticated, setIsTeacherAuthenticated] = useState(ApiService.isLoggedInTeacher()); - //const [isAdmin, setIsAdmin] = useState(false); + const [isAdmin, setIsAdmin] = useState(false); const [isRoomRequireAuthentication, setRoomsRequireAuth] = useState(null); const location = useLocation(); @@ -43,7 +43,7 @@ const App: React.FC = () => { const checkLoginStatus = () => { setIsAuthenticated(ApiService.isLoggedIn()); setIsTeacherAuthenticated(ApiService.isLoggedInTeacher()); - //setIsAdmin(ApiService.isAdmin()); + setIsAdmin(ApiService.isAdmin()); }; const fetchAuthenticatedRooms = async () => { @@ -63,7 +63,7 @@ const App: React.FC = () => { return (
-
+
diff --git a/client/src/services/ApiService.tsx b/client/src/services/ApiService.tsx index e0b498c..2661d24 100644 --- a/client/src/services/ApiService.tsx +++ b/client/src/services/ApiService.tsx @@ -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 { if (!username || username.length === 0) { return;