frigate/web/src/api/index.tsx
Josh Hawkins 130dc76a01 only redirect to login page once on 401
attempt to fix ios pwa safari redirect storm
2025-12-03 21:59:57 -06:00

64 lines
1.7 KiB
TypeScript

import { baseUrl } from "./baseUrl";
import { SWRConfig } from "swr";
import { WsProvider } from "./ws";
import axios from "axios";
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<string, unknown>;
};
export function ApiProvider({ children, options }: ApiProviderType) {
axios.defaults.headers.common = {
"X-CSRF-TOKEN": 1,
"X-CACHE-BYPASS": 1,
};
return (
<SWRConfig
value={{
fetcher: (key) => {
const [path, params] = Array.isArray(key) ? key : [key, undefined];
return axios.get(path, { params }).then((res) => res.data);
},
onError: (error, _key) => {
if (
error.response &&
[401, 302, 307].includes(error.response.status)
) {
// redirect to the login page if not already there
const loginPage = error.response.headers.get("location") ?? "login";
if (window.location.href !== loginPage && !isRedirectingToLogin) {
isRedirectingToLogin = true;
window.location.href = loginPage;
}
}
},
...options,
}}
>
<WsWithConfig>{children}</WsWithConfig>
</SWRConfig>
);
}
type WsWithConfigType = {
children: ReactNode;
};
function WsWithConfig({ children }: WsWithConfigType) {
return <WsProvider>{children}</WsProvider>;
}
// eslint-disable-next-line react-refresh/only-export-components
export function useApiHost() {
return baseUrl;
}