Hono + Bun gives a 404 on /api/auth/ok endpoint

Hey, I'm trying to setup better-auth on a hono server like this, but when I call the /api/auth/ok endpoint I got a 404. In fact I'm also using a app.route() in my entryfile. Working version
const app = new Hono()

const userRoute = new Hono()
// .get("/top", (c) => {
// return c.json({ message: "Top" }, 200)
// })
.get(
"/:id",
zValidator(
"param",
z.object({
id: z.string(),
})
),
(c) => {
return c.json({ message: `Hello ${c.req.param("id")}` }, 200)
}
)

const routes = app.route("/user", userRoute)

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

export default app
const app = new Hono()

const userRoute = new Hono()
// .get("/top", (c) => {
// return c.json({ message: "Top" }, 200)
// })
.get(
"/:id",
zValidator(
"param",
z.object({
id: z.string(),
})
),
(c) => {
return c.json({ message: `Hello ${c.req.param("id")}` }, 200)
}
)

const routes = app.route("/user", userRoute)

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

export default app
Non working version
const userRoute = new Hono()
.get("/top", (c) => {
return c.json({ message: "Top" }, 200)
}) // I've added this 😅😅😅😅
.get(
"/:id",
zValidator(
"param",
z.object({
id: z.string(),
})
),
(c) => {
return c.json({ message: `Hello ${c.req.param("id")}` }, 200)
}
)

const routes = app.route("/user", userRoute)

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

export default app
const userRoute = new Hono()
.get("/top", (c) => {
return c.json({ message: "Top" }, 200)
}) // I've added this 😅😅😅😅
.get(
"/:id",
zValidator(
"param",
z.object({
id: z.string(),
})
),
(c) => {
return c.json({ message: `Hello ${c.req.param("id")}` }, 200)
}
)

const routes = app.route("/user", userRoute)

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

export default app
It's really strange that this bug happen when I have a new endpoint declarated Has anyone had this problem before, or can anyone help me? Wishing you a good day 😉
5 Replies
Mattèo
MattèoOP•3mo ago
Problem solved the /api/auth/** handler is not well written, change it to /api/auth/* (PR that solve the issue in better-auth docs https://github.com/better-auth/better-auth/pull/1317)
Aziz
Aziz•3mo ago
Does re-ordering them work?
Mattèo
MattèoOP•3mo ago
Yes, I solved the issue by reordering them, checkout the code changes in this PR to fix the issue https://github.com/better-auth/better-auth/pull/1317
GitHub
docs: wrong pattern in hono integration code by MatteoGauthier · Pu...
Caused by c26285b The pattern should be * instead of ** since it's not a supported syntax It is causing strange issue then in the Hono server handlers Working version const app = new Hono()...
Aziz
Aziz•3mo ago
Actually ** seems to work for me But I am only using hono for auth and attaching a trpc instance to it as well
Mattèo
MattèoOP•3mo ago
Very strange ...

Did you find this page helpful?