forked from baron/baron-sso
74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
import axios from "axios";
|
|
import { shouldStartLoginRedirect } from "../../../common/core/auth";
|
|
import { shouldSuppressDevelopmentSessionRedirect } from "../../../common/core/session";
|
|
import { clearAdminAuthSession, userManager } from "./auth";
|
|
|
|
let isRedirectingToLogin = false;
|
|
|
|
const apiClient = axios.create({
|
|
baseURL: (window as Window & typeof globalThis & { _IS_TEST_MODE?: boolean })
|
|
._IS_TEST_MODE
|
|
? "http://playwright-mock/api"
|
|
: (import.meta.env.VITE_ADMIN_API_BASE ?? "/api"),
|
|
});
|
|
|
|
apiClient.interceptors.request.use(async (config) => {
|
|
// IdP 중립 Auth 레이어 연동: oidc-client의 userManager에서 최신 토큰을 가져옵니다.
|
|
const user = await userManager.getUser();
|
|
const sessionToken =
|
|
user?.access_token || window.localStorage.getItem("admin_session");
|
|
|
|
if (sessionToken) {
|
|
config.headers.Authorization = `Bearer ${sessionToken}`;
|
|
}
|
|
|
|
// 테넌트 선택 값을 보관하고 헤더로 전달한다.
|
|
const tenantId = window.localStorage.getItem("admin_tenant");
|
|
if (tenantId) {
|
|
config.headers["X-Tenant-ID"] = tenantId;
|
|
}
|
|
|
|
return config;
|
|
});
|
|
|
|
apiClient.interceptors.response.use(
|
|
(response) => response,
|
|
async (error) => {
|
|
if (error.response?.status === 401) {
|
|
if (
|
|
shouldSuppressDevelopmentSessionRedirect({
|
|
appMode: import.meta.env.MODE,
|
|
})
|
|
) {
|
|
console.warn(
|
|
"[apiClient] 401 Unauthorized detected, but development session redirects are disabled.",
|
|
);
|
|
return Promise.reject(error);
|
|
}
|
|
|
|
console.warn(
|
|
"[apiClient] 401 Unauthorized detected. Clearing session state.",
|
|
);
|
|
|
|
await clearAdminAuthSession();
|
|
|
|
if (
|
|
shouldStartLoginRedirect({
|
|
pathname: window.location.pathname,
|
|
isRedirecting: isRedirectingToLogin,
|
|
})
|
|
) {
|
|
isRedirectingToLogin = true;
|
|
console.info(
|
|
"[apiClient] Redirecting to /login from",
|
|
window.location.pathname,
|
|
);
|
|
window.location.href = "/login";
|
|
}
|
|
}
|
|
return Promise.reject(error);
|
|
},
|
|
);
|
|
|
|
export default apiClient;
|