40 lines
1.5 KiB
TypeScript
40 lines
1.5 KiB
TypeScript
import { apiError, createHttpClient, HttpError, redirectToLogout } from "@kri/common/http";
|
|
import { clientEnv } from "@/lib/env.client";
|
|
import { announceProjectAccess, projectFromRequest } from "@/lib/project-access";
|
|
import { readExternalAuth } from "@/lib/external-auth";
|
|
|
|
const resolveBaseUrl = () =>
|
|
typeof window === "undefined"
|
|
? clientEnv.API_BASE
|
|
: new URL(clientEnv.API_BASE, window.location.origin).toString();
|
|
|
|
export const nextApi = createHttpClient(resolveBaseUrl(), {
|
|
getToken: () => {
|
|
const external = readExternalAuth();
|
|
if (!external) return null;
|
|
if (!external.accessToken) {
|
|
redirectToLogout();
|
|
throw new HttpError(401, { message: "공유 링크 인증이 필요합니다." }, "");
|
|
}
|
|
return external.accessToken;
|
|
},
|
|
onForbidden: (error) => {
|
|
const info = apiError(error);
|
|
if (readExternalAuth() || info.code === "EXTERNAL_SCOPE_DENIED" || info.code === "PROJECT_IDENTITY_REQUIRED") {
|
|
redirectToLogout();
|
|
return;
|
|
}
|
|
const project = projectFromRequest(error.url);
|
|
if (info.code === "PROJECT_ACCESS_DENIED" && project && !new URL(error.url).pathname.endsWith("/access"))
|
|
announceProjectAccess({ project, allowed: false, message: info.message });
|
|
},
|
|
onResponseError: false,
|
|
});
|
|
|
|
// Login/logout BFF operations must also work before an External JWT is available.
|
|
export const authApi = createHttpClient(resolveBaseUrl(), {
|
|
onUnauthorized: false,
|
|
onForbidden: false,
|
|
onResponseError: false,
|
|
});
|