Skip to main content
Better Auth integrates with Hono by mounting the auth handler on a wildcard route. Before you start, make sure you have a Better Auth instance configured. If you haven’t done that yet, check out the installation.

Mount the handler

Use app.on to handle both GET and POST requests on the /api/auth/* path and forward them to auth.handler.
server.ts

CORS configuration

Use the cors middleware from hono/cors. Register it before your routes so cross-origin requests are handled before they reach the auth endpoints.
server.ts
CORS middleware must be registered before your routes. If CORS is registered after the route, preflight requests will fail.

Session middleware

Add a global middleware that fetches the session and stores the user and session objects in Hono’s context. This lets every downstream route access them without repeating the session lookup.
server.ts

Accessing session in routes

Cross-domain cookies

By default, all Better Auth cookies use SameSite=Lax. If your client and server share a subdomain, enable cross-subdomain cookies in your auth config:
auth.ts
If you need fully cross-origin cookies (SameSite=None), configure the default cookie attributes:
auth.ts
You can also override attributes on individual cookies:
auth.ts

Hono client configuration

When using the Hono RPC client (hono/client) to call Better Auth-protected endpoints, set credentials: "include" so the client forwards cookies on cross-origin requests.
lib/api.ts
credentials: "include" must be paired with credentials: true in the server’s CORS configuration, and the origin must be set to a specific domain (not *).