Skip to content

Commit

Permalink
Merge pull request #232 from Arquisoft/fix/webapp/refresh-continuous
Browse files Browse the repository at this point in the history
Fix the multiple petitions to /auth/refresh
  • Loading branch information
Toto-hitori authored Apr 16, 2024
2 parents 0dbfed7 + e91f00e commit faa545c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
12 changes: 9 additions & 3 deletions webapp/src/components/auth/AuthManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions webapp/src/components/utils/ProtectedRoute.jsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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");
Expand Down

0 comments on commit faa545c

Please sign in to comment.