[GH-ISSUE #648] Using Sveltekit: not passing baseURL in createAuthClient() causes crash when calling client.useSession() #8359

Closed
opened 2026-04-13 03:25:44 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @bfovez on GitHub (Nov 24, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/648

Describe the bug
Using Sveltekit.
If baseUrl option is not provided to the client, then the followin crash immediately occurs when calling client.useSession():

file:///C:/Users/_______/node_modules/@better-fetch/fetch/dist/index.js:455
    throw new TypeError(
          ^

TypeError: Invalid URL /get-session. Are you passing in a relative URL but not setting the baseURL?
    at getURL2 (file:///C:/Users/_______/node_modules/@better-fetch/fetch/dist/index.js:455:11)
    at betterFetch (file:///C:/Users/_______/node_modules/@better-fetch/fetch/dist/index.js:502:16)
    at async $fetch (file:///C:/Users/_______/node_modules/@better-fetch/fetch/dist/index.js:236:12)

Node.js v20.11.1

To Reproduce
Steps to reproduce the behavior:

  1. Get the Better-Auth Sveltekit example

  2. In the src/lib/authClient.ts file, change this:

export const client = createAuthClient({
	baseURL: "http://localhost:3000",
});

into this:

export const client = createAuthClient();
  1. Launch the server with npm run dev

  2. Visit http://localhost:3000/ and see the error in the console.

Expected behavior
No error.

Desktop (please complete the following information):

  • OS: Windows 11
  • Browser: Chrome
  • Version: Node 21
Originally created by @bfovez on GitHub (Nov 24, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/648 **Describe the bug** Using Sveltekit. If baseUrl option is not provided to the client, then the followin crash immediately occurs when calling `client.useSession()`: ``` file:///C:/Users/_______/node_modules/@better-fetch/fetch/dist/index.js:455 throw new TypeError( ^ TypeError: Invalid URL /get-session. Are you passing in a relative URL but not setting the baseURL? at getURL2 (file:///C:/Users/_______/node_modules/@better-fetch/fetch/dist/index.js:455:11) at betterFetch (file:///C:/Users/_______/node_modules/@better-fetch/fetch/dist/index.js:502:16) at async $fetch (file:///C:/Users/_______/node_modules/@better-fetch/fetch/dist/index.js:236:12) Node.js v20.11.1 ``` **To Reproduce** Steps to reproduce the behavior: 1. Get the [Better-Auth Sveltekit example](https://github.com/better-auth/better-auth/tree/main/examples/svelte-kit-example) 2. In the `src/lib/authClient.ts` file, change this: ```ts export const client = createAuthClient({ baseURL: "http://localhost:3000", }); ``` into this: ```ts export const client = createAuthClient(); ``` 4. Launch the server with `npm run dev` 5. Visit `http://localhost:3000/` and see the error in the console. **Expected behavior** No error. **Desktop (please complete the following information):** - OS: Windows 11 - Browser: Chrome - Version: Node 21
GiteaMirror added the locked label 2026-04-13 03:25:44 -05:00
Author
Owner

@Bekacru commented on GitHub (Nov 24, 2024):

That's because of SSR. The client tries to infer the base URL from window when it's on the client. If it's called on the server, it can't infer it, so it fails. I'm not sure if there's a consistent, easier way to get the base URL in SvelteKit. If there is, we should include that in our base URL search paths. If not, passing the base URL should be a fine alternative.

<!-- gh-comment-id:2496143985 --> @Bekacru commented on GitHub (Nov 24, 2024): That's because of SSR. The client tries to infer the base URL from `window` when it's on the client. If it's called on the server, it can't infer it, so it fails. I'm not sure if there's a consistent, easier way to get the base URL in SvelteKit. If there is, we should include that in our base URL search paths. If not, passing the base URL should be a fine alternative.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8359