auth.ts29 lines · main
1import { Hono } from 'hono';
2
3import { auth } from '../lib/auth.js';
4
5/**
6 * Mount the Better Auth handler under /v1/auth/*.
7 *
8 * Better Auth owns most /v1/auth/* paths (sign-up, sign-in, sign-out, magic
9 * link, OAuth callbacks, session, password reset, etc.).
10 *
11 * Exception: POST /v1/auth/cli-token is owned by authCliRouter and must be
12 * registered *before* this catch-all (see apps/api/src/index.ts).
13 */
14export const authRouter = new Hono();
15
16authRouter.on(['GET', 'POST'], '/v1/auth/*', (c) => {
17 // Defence-in-depth if mount order is ever inverted.
18 if (c.req.path === '/v1/auth/cli-token') {
19 return c.json(
20 {
21 code: 'route_order_error',
22 message:
23 'cli-token must be handled by authCliRouter — check mount order in index.ts',
24 },
25 500,
26 );
27 }
28 return auth.handler(c.req.raw);
29});