diff --git a/web/src/api/index.tsx b/web/src/api/index.tsx index a9044a6d7..540a328eb 100644 --- a/web/src/api/index.tsx +++ b/web/src/api/index.tsx @@ -6,6 +6,10 @@ import { ReactNode } from "react"; axios.defaults.baseURL = `${baseUrl}api/`; +// Module-level flag to prevent multiple simultaneous redirects +// (eg, when multiple SWR queries fail with 401 at once) +let isRedirectingToLogin = false; + type ApiProviderType = { children?: ReactNode; options?: Record; @@ -31,7 +35,8 @@ export function ApiProvider({ children, options }: ApiProviderType) { ) { // redirect to the login page if not already there const loginPage = error.response.headers.get("location") ?? "login"; - if (window.location.href !== loginPage) { + if (window.location.href !== loginPage && !isRedirectingToLogin) { + isRedirectingToLogin = true; window.location.href = loginPage; } }