Repository navigation
feat: add the server app and wire Better Auth to the schema - #2
Merged
Merged
Conversation
Add `apps/server`: a Hono application that mounts Better Auth at `/api/auth/*`, plus the Bun entry point that owns the connection pool. `authOptions` is now the single source of truth for the static Better Auth configuration, and the compatibility test derives its expectations from that same object rather than a second plugin list. This proves what `packages/db` could only assert structurally: Better Auth's own writes satisfy the prefixed-identifier CHECK constraints, and its adapter accepts the schema. The structural comparison moved here with the configuration, so `packages/db` no longer depends on Better Auth at all. `createAuth` rejects a secret shorter than 32 characters, which Better Auth only warns about, and every deployment adapter inherits that guard. `better-auth` and its Drizzle adapter are pinned exactly, because compatibility with a hand-maintained schema is version-specific and upgrading it is a deliberate change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
apps/server, a Hono application that mounts Better Auth at/api/auth/*. This is the missing half of #1, which landed the schema but had nothing that could exercise it.The thing this proves
packages/dbcould only check the schema structurally — it compared metadata and ran hand-written inserts. Nothing had ever shown that Better Auth itself can write these tables, which mattered because the identifier CHECK constraints reject any insert whose id isn't generated throughpackages/db/id.ts. A misconfiguredgenerateIdwould fail every write.apps/server/auth.test.tsnow runs real flows — credential sign-up, organization creation — against migrated PostgreSQL via PGlite, assertingusr_…,acc_…,ses_…,org_…,mem_…rows land. It also initializes the Drizzle adapter, so Better Auth's own start-up schema check runs in CI. I confirmed that check fails loudly rather than passing silently by breaking the schema deliberately: it throwsSchemaMismatchError.Verification, invitation, and two-factor writes are not exercised; the test header says so.
One source of truth for the auth configuration
authOptionsinapps/server/auth.tsis the static Better Auth configuration, and the compatibility test derives its expected tables from that same object:Previously
packages/dbdeclared its own plugin list to test against, which had to be kept in sync by hand. That structural comparison moved here with the configuration, sopackages/dbno longer depends onbetter-authat all — it holds the persistence schema Better Auth expects, while the configuration lives with the app.Security and dependency decisions
createAuththrows on a secret under 32 characters. Better Auth only logs a warning below that length, so a short secret would otherwise reach production behind a log line. The guard sits increateAuth, not the Bun entry, so a future Workers adapter inherits it.better-authand@better-auth/drizzle-adapterare pinned to exact versions. Compatibility with a hand-maintained schema is version-specific and nothing automatically compares column types or constraints against the library, so an upgrade must be a deliberate change rather than somethingbun updateperforms. ADR 0001 records the procedure.appName: "Quality Runtime"is set because it is the TOTP issuer authenticator apps display, andtwoFactor()is enabled.Deployment boundary
The Bun entry is named
bun.ts, notindex.ts, because it is deployment-specific: it reads the environment and owns a long-livedPool. A Workers entry would sit beside it and build a per-requestClientbehind Hyperdrive, which is the shape Cloudflare documents.createApp/createAuthtake their dependencies, so neither knows which one it is running under.Verification
bun run check,bun run test(69 passing), andreuse lintpass.bun run devboots and serves the routes.Not verified: no end-to-end run against a standalone PostgreSQL — the integration tests use PGlite, and
bun run devreaches a real server but stops at3D000(database does not exist) in this environment.