diff --git a/webapp/src/components/auth/AuthManager.js b/webapp/src/components/auth/AuthManager.js index 11bf90a4..b94db36e 100644 --- a/webapp/src/components/auth/AuthManager.js +++ b/webapp/src/components/auth/AuthManager.js @@ -76,10 +76,16 @@ export default class AuthManager { let response = await this.getAxiosInstance().post(process.env.REACT_APP_API_ENDPOINT + "/auth/refresh-token", { "refresh_token": localStorage.getItem("jwtRefreshToken") }); - this.#saveToken(response); - AuthManager.#instance.setLoggedIn(true); + if (response.status === HttpStatusCode.Ok) { + this.#saveToken(response); + AuthManager.#instance.setLoggedIn(true); + } } catch (error) { - console.error("Error refreshing token: ", error); + if (error.response.status === HttpStatusCode.Forbidden) { + localStorage.removeItem("jwtRefreshToken"); + } else { + console.error("Error refreshing token: ", error); + } } } diff --git a/webapp/src/components/utils/ProtectedRoute.jsx b/webapp/src/components/utils/ProtectedRoute.jsx index a7c49e06..8c2fe7c9 100644 --- a/webapp/src/components/utils/ProtectedRoute.jsx +++ b/webapp/src/components/utils/ProtectedRoute.jsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from "react"; +import React, { useEffect, useRef, useState } from "react"; import { Outlet, useNavigate } from "react-router-dom"; import AuthManager from "../auth/AuthManager"; import { CircularProgress } from "@chakra-ui/react"; @@ -7,10 +7,11 @@ const ProtectedRoutes = () => { const navigate = useNavigate(); const [hasLoaded, setHasLoaded] = useState(false); + const authManager = useRef(new AuthManager()); useEffect(() => { async function protectRoute() { - let isLoggedIn = await new AuthManager().isLoggedIn(); + let isLoggedIn = await authManager.current.isLoggedIn(); setHasLoaded(true); if (!(isLoggedIn)) { navigate("/login");