new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Better Auth · all subjects

client apis/express

7 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Express integration setup with toNodeHandler

To integrate Better Auth with Express, mount the handler to an API route using toNodeHandler(auth). For ExpressJS v4, use app.all("/api/auth/*", toNodeHandler(auth)). For ExpressJS v5, use app.all("/api/auth/*splat", toNodeHandler(auth)). Mount express.json() middleware after the Better Auth handler, or only apply it to routes that don't interact with Better Auth.

ESM required for Express integration

CommonJS (cjs) is not supported for Better Auth Express integration. Use ECMAScript Modules (ESM) by setting "type": "module" in package.json or configuring tsconfig.json to use ES modules.

Express integration pitfall: express.json() placement

Do not use express.json() before the Better Auth handler. Using it before the handler will cause the client API to get stuck on "pending". Mount express.json() only after the Better Auth handler or only for routes that don't interact with Better Auth.

Express integration verification endpoint

After setting up Better Auth with Express, verify that the server is running by sending a GET request to /ok endpoint (which maps to /api/auth/ok).

Express CORS configuration example

To add CORS support to Express with Better Auth, use the cors middleware and configure it with: origin (specify the frontend domain), methods (list allowed HTTP methods like GET, POST, PUT, DELETE), and credentials (set to true to allow cookies and authorization headers).

Express getSession example with fromNodeHeaders

To retrieve the user's session in an Express route, use the fromNodeHeaders helper to convert Node.js request headers to Better Auth format, then call auth.api.getSession({ headers: fromNodeHeaders(req.headers) }).

fromNodeHeaders helper function

The fromNodeHeaders function converts Node.js request headers to the Headers object format expected by Better Auth API methods.

Give your agent this brain