Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/utils/axios-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ export function createAxiosClient({
client.interceptors.request.use((config) => {
if (typeof window !== "undefined") {
config.headers.set("X-Origin-URL", window.location.href);
// On unauthenticated clients, attach a stable anonymous visitor id so the
// backend can support anonymous agent access (conversation grouping +
// ownership). Authenticated clients are identified by their token instead.
if (!token) {
// On unauthenticated requests, attach a stable anonymous visitor id so the
// backend can support anonymous agent access (conversation grouping + ownership).
// Authenticated requests are identified by their Authorization header instead.
if (!config.headers.get("Authorization")) {
config.headers.set("X-Base44-Anonymous-Id", getAnalyticsSessionId());
}
}
Expand Down
20 changes: 19 additions & 1 deletion tests/unit/anonymous-visitor-header.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ afterEach(() => {
vi.unstubAllGlobals();
});

async function captureRequestHeaders(token?: string) {
async function captureRequestHeaders(
token?: string,
{ tokenSetAfterConstruction }: { tokenSetAfterConstruction?: string } = {}
) {
const client = createAxiosClient({ baseURL: "https://api", token });
// Mirrors the common browser path: auth.setToken() applies the Authorization
// header on the client's defaults after the client has already been built.
if (tokenSetAfterConstruction) {
client.defaults.headers.common[
"Authorization"
] = `Bearer ${tokenSetAfterConstruction}`;
}
let captured: any;
client.defaults.adapter = async (config) => {
captured = config;
Expand Down Expand Up @@ -63,4 +73,12 @@ describe("anonymous visitor header", () => {
expect(headers.get("X-Base44-Anonymous-Id")).toBeFalsy();
expect(headers.get("Authorization")).toBe("Bearer a-real-token");
});

test("token set after construction (browser setToken path) omits the anonymous header", async () => {
const headers = await captureRequestHeaders(undefined, {
tokenSetAfterConstruction: "a-real-token",
});
expect(headers.get("Authorization")).toBe("Bearer a-real-token");
expect(headers.get("X-Base44-Anonymous-Id")).toBeFalsy();
});
});
Loading