Convex + Better Auth
Integrations

Hono

Using Hono with Convex + Better Auth

If you prefer to work with Hono instead of the default HttpRouter, Hono can replace the default authComponent.registerRoutes() method. Check out the Convex w/ Hono Stack article and the Better Auth Hono docs for more details.

You'll need to install the convex-helpers package if you haven't already.

Configuration

convex/http.ts
import { Hono } from "hono";
import { HonoWithConvex, HttpRouterWithHono } from "convex-helpers/server/hono";
import { ActionCtx } from "./_generated/server";
import { createAuth } from "./auth";

const app: HonoWithConvex<ActionCtx> = new Hono();

// Redirect root well-known to api well-known
app.get("/.well-known/openid-configuration", async (c) => {
  return c.redirect("/api/auth/convex/.well-known/openid-configuration");
});

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

const http = new HttpRouterWithHono(app);

export default http;

A handler mounted with Hono skips the origin checks in authComponent.registerRoutes() and passes the request's host to Better Auth unchanged. If your baseURL is dynamic, list only exact hosts you control in allowedHosts, because Better Auth builds emailed links from any request host that a wildcard matches.

Add CORS support

Required for client only / SPA installs like React/Vite.

convex/http.ts
import { Hono } from "hono";
import { HonoWithConvex, HttpRouterWithHono } from "convex-helpers/server/hono";
import { cors } from "hono/cors"; 
import { ActionCtx } from "./_generated/server";
import { createAuth } from "../lib/auth";

const app: HonoWithConvex<ActionCtx> = new Hono();

app.use(
  "/api/auth/*",
  cors({
    origin: process.env.SITE_URL,
    allowHeaders: ["Content-Type", "Authorization", "Better-Auth-Cookie"],
    allowMethods: ["GET", "POST", "OPTIONS"],
    exposeHeaders: ["Content-Length", "Set-Better-Auth-Cookie"],
    maxAge: 600,
    credentials: true,
  })
);

// ...

On this page