basePath set to root throws 404 on 2+ segment paths #1201

Closed
opened 2026-03-13 08:27:51 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @MrLightful on GitHub (May 13, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Create a basic hono backend:
https://www.better-auth.com/docs/integrations/hono
(don't know about other backends)

const app = new Hono();

app.use(
    "*",
    cors({
      origin: "http://localhost:1420",
      allowHeaders: ["Content-Type", "Authorization"],
      allowMethods: ["POST", "GET", "OPTIONS"],
      exposeHeaders: ["Content-Length"],
      maxAge: 600,
      credentials: true,
    }),
);

app.on(["POST", "GET"], "*", (c) => {
  return auth.handler(c.req.raw);
});

serve(app);

Current vs. Expected behavior

This works (as expected):

curl http://localhost:3000/get-session

This throws 404 (while expected 400):

curl http://localhost:3000/sign-up/email \
  --request POST \
  --header 'Content-Type: application/json' \
  --data '{
  "name": "",
  "email": "",
  "password": "",
  "callbackURL": ""
}'

And any other 2+ segment paths fail on 404.

What version of Better Auth are you using?

1.2.7

Provide environment information

- OS: MacOS

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  basePath: '/',
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @MrLightful on GitHub (May 13, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Create a basic hono backend: https://www.better-auth.com/docs/integrations/hono _(don't know about other backends)_ ``` const app = new Hono(); app.use( "*", cors({ origin: "http://localhost:1420", allowHeaders: ["Content-Type", "Authorization"], allowMethods: ["POST", "GET", "OPTIONS"], exposeHeaders: ["Content-Length"], maxAge: 600, credentials: true, }), ); app.on(["POST", "GET"], "*", (c) => { return auth.handler(c.req.raw); }); serve(app); ``` ### Current vs. Expected behavior This works (as expected): ``` curl http://localhost:3000/get-session ``` This throws 404 (while expected 400): ``` curl http://localhost:3000/sign-up/email \ --request POST \ --header 'Content-Type: application/json' \ --data '{ "name": "", "email": "", "password": "", "callbackURL": "" }' ``` And any other 2+ segment paths fail on 404. ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash - OS: MacOS ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ basePath: '/', emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
Author
Owner

@Kinfe123 commented on GitHub (May 13, 2025):

how are you handling the two segment routes like can you show me how you handling on the app.on for path matching ? or send the full entry file of the hono app

@Kinfe123 commented on GitHub (May 13, 2025): how are you handling the two segment routes like can you show me how you handling on the `app.on` for path matching ? or send the full entry file of the hono app
Author
Owner

@MrLightful commented on GitHub (May 13, 2025):

It's all i've got. The handler comes from better-auth. And the * matching in hono app is meant to redirect all requests to the handler.

And as soon as basePath has a non-root path, it works fine. So having basePath set to /a, all api endpoints work well incl. multi-segments.

@MrLightful commented on GitHub (May 13, 2025): It's all i've got. The handler comes from better-auth. And the `*` matching in hono app is meant to redirect all requests to the handler. And as soon as basePath has a non-root path, it works fine. So having `basePath` set to `/a`, all api endpoints work well incl. multi-segments.
Author
Owner

@Kinfe123 commented on GitHub (May 13, 2025):

yeah if you set a basePath to "/" you might need to mention the segment manully like '/sign-up' separately.

@Kinfe123 commented on GitHub (May 13, 2025): yeah if you set a basePath to "/" you might need to mention the segment manully like '/sign-up' separately.
Author
Owner

@Kinfe123 commented on GitHub (May 28, 2025):

When a URL already has a path, it returns the original URL without modification so can you guys make sure to pass the full url path component with the baseUrl if you have to and make sure you guys mounted on the right path by default the one on the docs is /api/[...auth] here which is based on the default config make sure ot change that up based on your needs.

@Kinfe123 commented on GitHub (May 28, 2025): When a URL already has a path, it returns the original URL without modification so can you guys make sure to pass the full url path component with the baseUrl if you have to and make sure you guys mounted on the right path by default the one on the docs is /api/[...auth] here which is based on the default config make sure ot change that up based on your needs.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1201