[GH-ISSUE #1432] Should baseUrl be a required parameter for createAuthClient? #8751

Closed
opened 2026-04-13 03:57:11 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @vladinator1000 on GitHub (Feb 13, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1432

Hey folks, I just stumbled on a deceptive bug while developing a React Router 7 application.

The client docs say you can do this

import { createAuthClient } from "better-auth/react
 
export const authClient = createAuthClient({
    // client configuration here
})

But in actuality it breaks at runtime when you try to log in in some scenarios unless you give it a baseUrl (with this error TypeError: Invalid URL /sign-in/social. Are you passing in a relative URL but not setting the baseURL?)

import { createAuthClient } from "better-auth/react"
 
export const authClient = createAuthClient({
   baseUrl: '' // <-- now you have to worry about where to get this from
})

I'm not sure how to exactly reproduce the issue, but for me it was working on my desktop browser on the deployed site on Cloudflare workers, but not from my phone.

The approach that worked for me to avoid this error is to create the auth client in a server action, because that's the only place I get access to the configuration

export async function action({ request, context }: Route.ActionArgs) {
  const authClient = createAuthClient({baseUrl: context.config.clientUrl })
  const formData = await request.formData()
  // ...
}

If the baseUrl client param being optional for the client leads to runtime bugs like this, do you think it would be better if it was required?

I'm also looking at the api client but it has slightly worse ergonomics:

await auth.api.signInEmail({
    body: {
        email: "",
        password: ""
    }
})

Also the integration docs all use the react client. So users would have to translate types and call signatures instead of copy pasting.

Originally created by @vladinator1000 on GitHub (Feb 13, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1432 Hey folks, I just stumbled on a deceptive bug while developing a React Router 7 application. The [client docs](https://better-auth.vercel.app/docs/concepts/client) say you can do this ```ts import { createAuthClient } from "better-auth/react export const authClient = createAuthClient({ // client configuration here }) ``` But in actuality it breaks at runtime when you try to log in in some scenarios unless you give it a `baseUrl` (with this error `TypeError: Invalid URL /sign-in/social. Are you passing in a relative URL but not setting the baseURL?`) ```ts import { createAuthClient } from "better-auth/react" export const authClient = createAuthClient({ baseUrl: '' // <-- now you have to worry about where to get this from }) ``` I'm not sure how to exactly reproduce the issue, but for me it was working on my desktop browser on the deployed site on Cloudflare workers, but not from my phone. The approach that worked for me to avoid this error is to create the auth client in a [server action](https://reactrouter.com/start/framework/actions#server-actions), because that's the only place I get access to the configuration ```ts export async function action({ request, context }: Route.ActionArgs) { const authClient = createAuthClient({baseUrl: context.config.clientUrl }) const formData = await request.formData() // ... } ``` If the `baseUrl` client param being optional for the client leads to runtime bugs like this, do you think it would be better if it was required? I'm also looking at [the api client](https://better-auth.vercel.app/docs/concepts/api) but it has slightly worse ergonomics: ```ts await auth.api.signInEmail({ body: { email: "", password: "" } }) ``` Also the integration docs all [use the react client](https://www.better-auth.com/docs/authentication/google#sign-in-with-google). So users would have to translate types and call signatures instead of copy pasting.
GiteaMirror added the locked label 2026-04-13 03:57:11 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8751