hono with better-auth

It should be a pretty straightforward setup, all I'm trying to do here is get the basic authentication working with better auth, I have the schema generated and everything but I keep having issues actually hitting the /api/auth routes. Any idea why this might be an issue? This is my setup: => app.ts
const app = createApp();

initializeLeaderboardRoutes(container.leaderboardController);
initializeHealthRoutes(container.healthController);
initializePlayerRoutes(container.playersController);

configureOpenAPI(app);


const routes = [
indexRoute,
leaderboardRoutes,
healthRoutes,
playerRoutes,
];

routes.forEach((route) => {
app.route("/v1", route);
});

app.all('/api/auth/**', async (c) => {
const res = await auth.handler(c.req.raw)
return res
})

export default app;
const app = createApp();

initializeLeaderboardRoutes(container.leaderboardController);
initializeHealthRoutes(container.healthController);
initializePlayerRoutes(container.playersController);

configureOpenAPI(app);


const routes = [
indexRoute,
leaderboardRoutes,
healthRoutes,
playerRoutes,
];

routes.forEach((route) => {
app.route("/v1", route);
});

app.all('/api/auth/**', async (c) => {
const res = await auth.handler(c.req.raw)
return res
})

export default app;
=> auth.ts
import db from "@/infrastructure/database";
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
}),
emailAndPassword: { enabled: true },
});
import db from "@/infrastructure/database";
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
}),
emailAndPassword: { enabled: true },
});
10 Replies
Boomslang
BoomslangOP2d ago
Also its important to note that the 404 response I'm getting is actually from the better-auth handler not my hono app.
Boomslang
BoomslangOP2d ago
No description
Marselena
Marselena2d ago
Hi! Unless I'm reading your logs incorrectly, from your screenshot it looks like you're testing this by sending a GET request to /api/auth? I don't think the base endpoint has any functionality with better auth - what happens if you try POST /api/auth/sign-up/email like in the docs?
Boomslang
BoomslangOP2d ago
I actually figured that out yes, when I hit sign up and sign in it actually work (POST), but doesn't better auth provide like GET requests? /session for example? I tried GET /api/auth/session that also gave me 404
Marselena
Marselena2d ago
try GET /api/auth/get-session
Boomslang
BoomslangOP2d ago
Well apparently I was not that smart, it works yeah.
Marselena
Marselena2d ago
haha the better auth docs are pretty opaque when it comes to explaining what endpoints their built in client helper is actually calling - I can't find /get-session actually anywhere in the docs - it's only here I found it
Boomslang
BoomslangOP2d ago
you are amazing, thanks for the help I'll need that link as well hah
Marselena
Marselena2d ago
GitHub
better-auth/packages/better-auth/src/client/session-atom.ts at main...
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
Boomslang
BoomslangOP2d ago
works perfectly, thank you!

Did you find this page helpful?