From c04558e13972ffb2bd9c9c3c749b9a7aa369d5bb Mon Sep 17 00:00:00 2001 From: Bruno Roesner Date: Sat, 19 Oct 2024 18:31:43 -0400 Subject: [PATCH] added frontend room authentication navigation --- client/src/App.tsx | 9 ++++++++- client/src/services/ApiService.tsx | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index 5199afa..3fad6fd 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -28,6 +28,7 @@ import OAuthCallback from './pages/AuthManager/callback/AuthCallback'; const App: React.FC = () => { const [isAuthenticated, setIsAuthenticated] = useState(ApiService.isLoggedIn()); const [isTeacherAuthenticated, setIsTeacherAuthenticated] = useState(ApiService.isLoggedInTeacher()); + const [isRoomRequireAuthentication, setRoomsRequireAuth] = useState(null); const location = useLocation(); // Check login status every time the route changes @@ -37,7 +38,13 @@ const App: React.FC = () => { setIsTeacherAuthenticated(ApiService.isLoggedInTeacher()); }; + const fetchAuthenticatedRooms = async () => { + const data = await ApiService.getRoomsRequireAuth(); + setRoomsRequireAuth(data); + }; + checkLoginStatus(); + fetchAuthenticatedRooms(); }, [location]); const handleLogout = () => { @@ -76,7 +83,7 @@ const App: React.FC = () => { {/* Pages espace étudiant */} : } + element={( !isRoomRequireAuthentication || isAuthenticated ) ? : } /> {/* Pages authentification */} diff --git a/client/src/services/ApiService.tsx b/client/src/services/ApiService.tsx index d5c0b28..909f29b 100644 --- a/client/src/services/ApiService.tsx +++ b/client/src/services/ApiService.tsx @@ -105,6 +105,17 @@ class ApiService { } } + // Route to know if rooms need authentication to join + public async getRoomsRequireAuth(): Promise { + const url: string = this.constructRequestUrl(`/auth/getRoomsRequireAuth`); + const result: AxiosResponse = await axios.get(url); + + if (result.status == 200) { + return result.data.roomsRequireAuth; + } + return false; + } + public logout(): void { return localStorage.removeItem("jwt"); }