From c15140549e1d314ee4836a4e24fdd7a2b2555ca9 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Thu, 18 Sep 2025 21:34:19 -0700 Subject: [PATCH] fix(client): baseURL is undefined for SSR (#4760) --- .../better-auth/src/client/client-ssr.test.ts | 25 +++++++++++++++++++ packages/better-auth/src/client/config.ts | 3 ++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 packages/better-auth/src/client/client-ssr.test.ts diff --git a/packages/better-auth/src/client/client-ssr.test.ts b/packages/better-auth/src/client/client-ssr.test.ts new file mode 100644 index 0000000000..4b0441144d --- /dev/null +++ b/packages/better-auth/src/client/client-ssr.test.ts @@ -0,0 +1,25 @@ +// @vitest-environment node +import { expect, it, vi } from "vitest"; +import { createAuthClient as createVueClient } from "./vue"; + +it("should call '/api/auth' if no baseURL is provided", async () => { + const customFetchImpl = vi.fn(async (url: string | Request | URL) => { + expect(url).toBe("/api/auth/get-session"); + return new Response(); + }); + const originalEnv = process.env; + process.env = {}; + // use DisposableStack when Node.js 24 is the minimum requirement + using _ = { + [Symbol.dispose]() { + process.env = originalEnv; + }, + }; + const client = createVueClient({ + fetchOptions: { + customFetchImpl, + }, + }); + await client.getSession(); + expect(customFetchImpl).toBeCalled(); +}); diff --git a/packages/better-auth/src/client/config.ts b/packages/better-auth/src/client/config.ts index b62cd8ddc8..1764d265e0 100644 --- a/packages/better-auth/src/client/config.ts +++ b/packages/better-auth/src/client/config.ts @@ -9,7 +9,8 @@ import { parseJSON } from "./parser"; export const getClientConfig = (options?: ClientOptions) => { /* check if the credentials property is supported. Useful for cf workers */ const isCredentialsSupported = "credentials" in Request.prototype; - const baseURL = getBaseURL(options?.baseURL, options?.basePath); + const baseURL = + getBaseURL(options?.baseURL, options?.basePath) ?? "/api/auth"; const pluginsFetchPlugins = options?.plugins ?.flatMap((plugin) => plugin.fetchPlugins)