diff --git a/src/frontend/src/components/authAdminGuard/index.tsx b/src/frontend/src/components/authAdminGuard/index.tsx index ece2248fa..96c404af3 100644 --- a/src/frontend/src/components/authAdminGuard/index.tsx +++ b/src/frontend/src/components/authAdminGuard/index.tsx @@ -7,14 +7,10 @@ export const ProtectedAdminRoute = ({ children }) => { useContext(AuthContext); if (!isAuthenticated) { - logout().then(() => { - return ; - }); - } - - if ((userData && !isAdmin) || autoLogin) { + logout(); + } else if ((userData && !isAdmin) || autoLogin) { return ; + } else { + return children; } - - return children; }; diff --git a/src/frontend/src/components/authGuard/index.tsx b/src/frontend/src/components/authGuard/index.tsx index 885f8fe93..8450248a9 100644 --- a/src/frontend/src/components/authGuard/index.tsx +++ b/src/frontend/src/components/authGuard/index.tsx @@ -1,14 +1,11 @@ import { useContext } from "react"; -import { Navigate } from "react-router-dom"; import { AuthContext } from "../../contexts/authContext"; export const ProtectedRoute = ({ children }) => { const { isAuthenticated, logout } = useContext(AuthContext); if (!isAuthenticated) { - logout().then(() => { - return ; - }); + logout(); + } else { + return children; } - - return children; }; diff --git a/src/frontend/src/components/headerComponent/index.tsx b/src/frontend/src/components/headerComponent/index.tsx index caae00b75..2b934942a 100644 --- a/src/frontend/src/components/headerComponent/index.tsx +++ b/src/frontend/src/components/headerComponent/index.tsx @@ -193,9 +193,7 @@ export default function Header(): JSX.Element { { - logout().then(() => { - navigate("/login"); - }); + logout(); }} > Sign Out diff --git a/src/frontend/src/contexts/authContext.tsx b/src/frontend/src/contexts/authContext.tsx index 9ebb34756..be2f279c9 100644 --- a/src/frontend/src/contexts/authContext.tsx +++ b/src/frontend/src/contexts/authContext.tsx @@ -1,4 +1,5 @@ import { createContext, useEffect, useState } from "react"; +import { useNavigate } from "react-router-dom"; import Cookies from "universal-cookie"; import { autoLogin as autoLoginApi, @@ -29,6 +30,7 @@ const initialValue: AuthContextType = { export const AuthContext = createContext(initialValue); export function AuthProvider({ children }): React.ReactElement { + const navigate = useNavigate(); const cookies = new Cookies(); const [accessToken, setAccessToken] = useState( cookies.get("access_token_lf") ?? null @@ -109,6 +111,7 @@ export function AuthProvider({ children }): React.ReactElement { setUserData(null); setAccessToken(null); setIsAuthenticated(false); + navigate("/login"); } catch (error) { console.error(error); throw error; diff --git a/src/frontend/src/controllers/API/api.tsx b/src/frontend/src/controllers/API/api.tsx index 4e9151f15..c6786cc1c 100644 --- a/src/frontend/src/controllers/API/api.tsx +++ b/src/frontend/src/controllers/API/api.tsx @@ -28,9 +28,7 @@ function ApiInterceptor() { authenticationErrorCount = authenticationErrorCount + 1; if (authenticationErrorCount > 3) { authenticationErrorCount = 0; - logout().then(() => { - navigate("/login"); - }); + logout(); } try { const res = await renewAccessToken(); @@ -47,14 +45,10 @@ function ApiInterceptor() { } } catch (error) { if (axios.isAxiosError(error) && error.response?.status === 401) { - logout().then(() => { - navigate("/login"); - }); + logout(); } else { console.error(error); - logout().then(() => { - navigate("/login"); - }); + logout(); } } } @@ -62,9 +56,7 @@ function ApiInterceptor() { if (!accessToken && error?.config?.url?.includes("login")) { return Promise.reject(error); } else { - logout().then(() => { - navigate("/login"); - }); + logout(); } } else { // if (URL_EXCLUDED_FROM_ERROR_RETRIES.includes(error.config?.url)) {