diff --git a/packages/lib/api.ts b/packages/lib/api.ts index 7cf8755f..9375347a 100644 --- a/packages/lib/api.ts +++ b/packages/lib/api.ts @@ -443,8 +443,18 @@ export const apiSchema = createSchema({ }, }) +function normalizeBackendV3BaseURL(rawBaseURL: string): string { + // Allow passing either "https://api.supermemory.ai" or "https://api.supermemory.ai/v3" + // (and tolerate trailing slashes) without accidentally producing "/v3/v3". + const trimmed = rawBaseURL.replace(/\/+$/, "") + return trimmed.endsWith("/v3") ? trimmed : `${trimmed}/v3` +} + +const backendBaseURL = + process.env.NEXT_PUBLIC_BACKEND_URL ?? "https://api.supermemory.ai" + export const $fetch = createFetch({ - baseURL: `${process.env.NEXT_PUBLIC_BACKEND_URL ?? "https://api.supermemory.ai"}/v3`, + baseURL: normalizeBackendV3BaseURL(backendBaseURL), credentials: "include", headers: { "X-App-Source": "nova" }, onRequest: (context: { headers: Headers }) => { diff --git a/packages/lib/auth.ts b/packages/lib/auth.ts index af0e989b..560f15e3 100644 --- a/packages/lib/auth.ts +++ b/packages/lib/auth.ts @@ -10,8 +10,18 @@ import { } from "better-auth/client/plugins" import { createAuthClient } from "better-auth/react" +function normalizeBackendBaseURL(rawBaseURL: string): string { + // Accept either "https://api.supermemory.ai" or "https://api.supermemory.ai/v3". + // Auth endpoints are mounted at the root base (not under /v3). + const trimmed = rawBaseURL.replace(/\/+$/, "") + return trimmed.endsWith("/v3") ? trimmed.slice(0, -3) : trimmed +} + +const backendBaseURL = + process.env.NEXT_PUBLIC_BACKEND_URL ?? "https://api.supermemory.ai" + export const authClient = createAuthClient({ - baseURL: process.env.NEXT_PUBLIC_BACKEND_URL ?? "https://api.supermemory.ai", + baseURL: normalizeBackendBaseURL(backendBaseURL), fetchOptions: { credentials: "include", headers: { "X-App-Source": "nova" },