only redirect to login page once on 401

attempt to fix ios pwa safari redirect storm
This commit is contained in:
Josh Hawkins 2025-12-03 21:59:57 -06:00
parent 87e67d22e2
commit 130dc76a01

View File

@ -6,6 +6,10 @@ import { ReactNode } from "react";
axios.defaults.baseURL = `${baseUrl}api/`; 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 = { type ApiProviderType = {
children?: ReactNode; children?: ReactNode;
options?: Record<string, unknown>; options?: Record<string, unknown>;
@ -31,7 +35,8 @@ export function ApiProvider({ children, options }: ApiProviderType) {
) { ) {
// redirect to the login page if not already there // redirect to the login page if not already there
const loginPage = error.response.headers.get("location") ?? "login"; 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; window.location.href = loginPage;
} }
} }