[GH-ISSUE #1499] Config request: ability to configure the "Return to Application" link of the Better Auth Error html page #8791

Closed
opened 2026-04-13 03:59:50 -05:00 by GiteaMirror · 8 comments
Owner

Originally created by @CorentinTh on GitHub (Feb 19, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1499

Is this suited for github?

  • Yes, this is suited for github

Not a problem, just a configuration request

Describe the solution you'd like

I'm looking for the ability to customize the link of the Return to Application button of the Better Auth Error page.
By default it is hard codded as / in 261fe93173/packages/better-auth/src/api/routes/error.ts (L89)

Usefull for client-server architecture, where the server is just a REST api

Maybe by adding a clientBaseUrl in the config of a more specific option like errorPage.returnToApplicationUrl ?

{
  // ...
  clientBaseUrl: 'https://...'
  // or
  errorPage: { returnToApplicationUrl: 'https://...'  }
  // ...
}

Image

Describe alternatives you've considered

Alternatives

  • As it's redirecting to / of the server by default, I'm considering redirecting the / of my server to my client frontend url
  • Other option is to add a before hook matching the /error route , to bypass this error page and direct to client

Additional context

I may be open to make the PR depending on the adopted solution

Originally created by @CorentinTh on GitHub (Feb 19, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1499 ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. Not a problem, just a configuration request ### Describe the solution you'd like I'm looking for the ability to customize the link of the `Return to Application` button of the `Better Auth Error` page. By default it is hard codded as `/` in https://github.com/better-auth/better-auth/blob/261fe931736e296e55885a4ab782e345199035e4/packages/better-auth/src/api/routes/error.ts#L89 Usefull for client-server architecture, where the server is just a REST api Maybe by adding a `clientBaseUrl` in the config of a more specific option like `errorPage.returnToApplicationUrl` ? ```jsonc { // ... clientBaseUrl: 'https://...' // or errorPage: { returnToApplicationUrl: 'https://...' } // ... } ``` ![Image](https://github.com/user-attachments/assets/82bde5af-5c17-4acc-9671-ea5180cb7994) ### Describe alternatives you've considered Alternatives - As it's redirecting to `/` of the server by default, I'm considering redirecting the `/` of my server to my client frontend url - Other option is to add a before hook matching the `/error` route , to bypass this error page and direct to client ### Additional context I may be open to make the PR depending on the adopted solution
GiteaMirror added the locked label 2026-04-13 03:59:50 -05:00
Author
Owner

@Bekacru commented on GitHub (Feb 20, 2025):

The error page should rarely be displayed. Its purpose is to provide a fallback when the backend doesn’t know where to redirect. For example, if you haven’t provided an errorCallbackURL when calling /sign-in/social and an error occurs during the OAuth callback.

That said we still can improve client fallback URL. For example, using the origin of the callbackURL instead of redirecting to the server if error happens in OAuth callback. The problem with assigning clientBaseURL or something similar is that a single auth instance can be used for multiple clients.

<!-- gh-comment-id:2670625239 --> @Bekacru commented on GitHub (Feb 20, 2025): The error page should rarely be displayed. Its purpose is to provide a fallback when the backend doesn’t know where to redirect. For example, if you haven’t provided an `errorCallbackURL` when calling `/sign-in/social` and an error occurs during the OAuth callback. That said we still can improve client fallback URL. For example, using the origin of the `callbackURL` instead of redirecting to the server if error happens in OAuth callback. The problem with assigning `clientBaseURL` or something similar is that a single auth instance can be used for multiple clients.
Author
Owner

@CorentinTh commented on GitHub (Feb 23, 2025):

This error page can also be shown when their is issues with the DB layer or when one "before" hooks raise and error.

Typically I often desactivate account creation in my app (but still allow login), I have a condition in the before-user-creation-hook, but the thing is, if one person, who does not have an account, tries to login using OAuth, better auth will tries to create a account (as we cannot distinguish login and registration in oauth here) and the person will face the error page as an error is raised

<!-- gh-comment-id:2676743846 --> @CorentinTh commented on GitHub (Feb 23, 2025): This error page can also be shown when their is issues with the DB layer or when one "before" hooks raise and error. Typically I often desactivate account creation in my app (but still allow login), I have a [condition in the before-user-creation-hook](https://github.com/papra-hq/owlrelay/blob/fd7955c7748bf5a22bb8efe5025e7d20f990810a/apps/server/src/modules/app/auth/auth.services.ts#L78), but the thing is, if one person, who does not have an account, tries to login using OAuth, better auth will tries to create a account (as we cannot distinguish login and registration in oauth here) and the person will face the error page as an error is raised
Author
Owner

@rohittiwari-dev commented on GitHub (Feb 26, 2025):

@Bekacru it is great to have this level of customization to have callback set for error and new user, but i think the code as of now has bug

Issue :
when user cancels the signing process errorCallbackURL is not called or app is not redirected to this page or url insted it redirects to
http://localhost:3000/api/auth/error?error=access_denied&error_description=undefined

here is detailed explaination i opened the issue if it is fixed then you can coment it there i will close it
https://github.com/better-auth/better-auth/issues/1580#issue-2882002654

<!-- gh-comment-id:2685422963 --> @rohittiwari-dev commented on GitHub (Feb 26, 2025): @Bekacru it is great to have this level of customization to have callback set for error and new user, but i think the code as of now has bug Issue : when user cancels the signing process `errorCallbackURL` is not called or app is not redirected to this page or url insted it redirects to `http://localhost:3000/api/auth/error?error=access_denied&error_description=undefined` here is detailed explaination i opened the issue if it is fixed then you can coment it there i will close it https://github.com/better-auth/better-auth/issues/1580#issue-2882002654
Author
Owner

@dosubot[bot] commented on GitHub (Jun 14, 2025):

Hi, @CorentinTh. I'm Dosu, and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • Feature request to customize the "Return to Application" link on the Better Auth Error page.
  • Current link is hardcoded to redirect to the server's root, which is not ideal for client-server architectures.
  • Discussion includes scenarios where the error page appears and a related bug with errorCallbackURL.
  • No resolution has been reached yet.

Next Steps:

  • Please confirm if this issue is still relevant to the latest version of the better-auth repository by commenting here.
  • If there is no response, the issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

<!-- gh-comment-id:2972847899 --> @dosubot[bot] commented on GitHub (Jun 14, 2025): Hi, @CorentinTh. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale. **Issue Summary:** - Feature request to customize the "Return to Application" link on the Better Auth Error page. - Current link is hardcoded to redirect to the server's root, which is not ideal for client-server architectures. - Discussion includes scenarios where the error page appears and a related bug with `errorCallbackURL`. - No resolution has been reached yet. **Next Steps:** - Please confirm if this issue is still relevant to the latest version of the better-auth repository by commenting here. - If there is no response, the issue will be automatically closed in 7 days. Thank you for your understanding and contribution!
Author
Owner

@JackNeto commented on GitHub (Aug 22, 2025):

This is still an issue, not sure why it was closed

<!-- gh-comment-id:3215448445 --> @JackNeto commented on GitHub (Aug 22, 2025): This is still an issue, not sure why it was closed
Author
Owner

@jtomaszewski commented on GitHub (Nov 14, 2025):

Just discovered onAPIError.errorURL in https://www.better-auth.com/docs/reference/options#onapierror - maybe this can help?

<!-- gh-comment-id:3531620243 --> @jtomaszewski commented on GitHub (Nov 14, 2025): Just discovered `onAPIError.errorURL` in https://www.better-auth.com/docs/reference/options#onapierror - maybe this can help?
Author
Owner

@jxri commented on GitHub (Nov 14, 2025):

You can set up hooks to redirect any /api/auth/error requests to your custom error page as a workaround:

export const auth = betterAuth({
    hooks: {
      before: createAuthMiddleware(async (ctx) => {
        if (ctx.path === "/error") {
            const queryString = new URLSearchParams(ctx.query as any).toString();
            throw ctx.redirect(`/login?${queryString}`)
          }
  
          return ctx;
      }),
  },
});
<!-- gh-comment-id:3532620799 --> @jxri commented on GitHub (Nov 14, 2025): You can set up `hooks` to redirect any `/api/auth/error` requests to your custom error page as a workaround: ```js export const auth = betterAuth({ hooks: { before: createAuthMiddleware(async (ctx) => { if (ctx.path === "/error") { const queryString = new URLSearchParams(ctx.query as any).toString(); throw ctx.redirect(`/login?${queryString}`) } return ctx; }), }, });
Author
Owner

@jtomaszewski commented on GitHub (Nov 17, 2025):

Thanks @jxri , it works.

<!-- gh-comment-id:3541924465 --> @jtomaszewski commented on GitHub (Nov 17, 2025): Thanks @jxri , it works.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8791