supermemory/packages/lib/auth.ts
gilangjavier cbe6075fc8 fix(lib): normalize backend baseURL to avoid /v3/v3
Prevents invalid API URLs when NEXT_PUBLIC_BACKEND_URL already includes /v3 (fixes dashboard JSON parse errors).
2026-03-25 20:16:51 +07:00

42 lines
1.1 KiB
TypeScript

import {
adminClient,
anonymousClient,
apiKeyClient,
emailOTPClient,
magicLinkClient,
organizationClient,
usernameClient,
} 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: normalizeBackendBaseURL(backendBaseURL),
fetchOptions: {
credentials: "include",
throw: true,
},
plugins: [
usernameClient(),
magicLinkClient(),
emailOTPClient(),
apiKeyClient(),
adminClient(),
organizationClient(),
anonymousClient(),
],
})
export const signIn = authClient.signIn
export const signOut = authClient.signOut
export const useSession = authClient.useSession
export const getSession = authClient.getSession