Better Auth
Use Better Auth, an open-source library in your app, for MainSDK auth - database, secret, main.yaml, local testing and swapping.
Better Auth is an open-source TypeScript authentication library that runs inside your app and keeps users in your own database. Behind auth, it gives you sign-in with no hosted identity service to sign up for. Pick it when you want users in your own Postgres and no third-party user directory. The auth spec proposes Better Auth and Clerk as the two drivers that make auth stable in 0.3.
Interfaces it serves
Pick Better Auth in main.yaml for any of these. Your calls stay the same, and you can swap later.
authBuilding for 0.3Driver better-auth Better Auth
auth:
driver: better-authSwap to another provider
Before you start
There is no account to create; you run Better Auth yourself.
- Pick the database that holds users and sessions, such as your Postgres, and apply Better Auth's schema to it.
- Generate a long random secret that Better Auth uses to sign and encrypt its tokens, one per environment.
- Register any social providers you offer, such as a GitHub OAuth app, and keep their client secrets.
Configure
auth:
driver: better-auth
methods: [passkeys]
# [to be decided] key names; suggested secrets: BETTER_AUTH_SECRET and the users database connectionThe auth spec does not define the driver's credential keys yet; they take secret: references when it does.
Use it
import { auth } from "mainsdk";
export async function GET(req: Request) {
const session = await auth.require(req); // 401 with MAIN-AUTH-0001 if not signed in
return Response.json(session.user);
}Local development and testing
Use the local auth driver in development, or run Better Auth against a local database. In tests, createTestApp() from @mainsdk/testing puts auth on its in-memory fake, and chaos.on("auth", { outage: true }) checks that auth.require() fails closed.
What MainSDK adds on top
- Short-lived, HttpOnly, Secure, SameSite sessions with rotating refresh tokens.
- Passkeys by default, phishing-resistant.
- Fails closed when the driver cannot reach its store.
- Every recovery audited, never weaker than sign-in.
See Sign-in and sessions.
Limits and notes
- Users live in your database, so their residency follows where you host it; MainSDK's regions are US, EU and India.
- The Better Auth driver is in the
authspec's first driver ticket with Clerk, held to the auth conformance suite. auth.raw()returns the library's instance for featuresauthdoes not wrap;mainsdk checklists every use.
Swap to or from it
auth:
driver: clerk # was better-authFAQ
Do I need a Better Auth account? No. It is a library; you need a database and a secret.
Can I keep my existing Better Auth setup? Yes. The driver runs on your existing tables (byo-identity).
Next
Was this page helpful?