Files
frigate/web/src/api/index.tsx
T
e7164882f5 Return to the requested URL after logging in (#24637)
When an unauthenticated user opens a deep link, carry the requested
path through the login page in a redirect query param and return to it
after a successful login instead of always going to the home page.
Only same-origin targets are honored.


Claude-Session: https://claude.ai/code/session_011CFGjgYURSRTpXBaxp1Dq7

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 17:59:13 -06:00

65 lines
1.7 KiB
TypeScript

import { baseUrl } from "./baseUrl";
import { SWRConfig } from "swr";
import { WsProvider } from "./WsProvider";
import axios from "axios";
import { ReactNode } from "react";
import {
getLoginUrl,
isRedirectingToLogin,
setRedirectingToLogin,
} from "./auth-redirect";
axios.defaults.baseURL = `${baseUrl}api/`;
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()) {
setRedirectingToLogin(true);
window.location.href = getLoginUrl(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;
}