mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
ajout isadmin check
This commit is contained in:
parent
0ae9791e5b
commit
10169b93f1
2 changed files with 24 additions and 3 deletions
|
|
@ -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>
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue