Uh oh!
There was an error while loading. Please reload this page.
Build Better Auth per request instead of at module eval - #64
Merged
Conversation
betterAuth() was called at module scope, so the Worker froze its secret, baseURL, and Google credentials at module-evaluation time — before OpenNext populates process.env from the Cloudflare request environment. In production that captured undefined values and left auth non-functional. buildAuthOptions()/getAuth() defer construction to the first request, and callers move from the `auth` singleton to getAuth(). Also adds the Better Auth D1 tables (singular `user`, `session`, `account`, `verification`, kept separate from the app's existing plural `users` table) plus unit tests for the option builder and the signed-out user menu. The sign-in callback moves from '/' to '/dashboard' so a fresh sign-in lands somewhere useful. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The route now resolves auth per request through getAuth(), so the existing module mock exported a symbol the route no longer imports and vitest failed with: No "getAuth" export is defined on the "@/lib/auth" mock. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Uh oh!
There was an error while loading. Please reload this page.
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 freeto 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.
Recovered from the local working tree, where it had been sitting uncommitted.
The bug
betterAuth()was called at module scope:OpenNext populates
process.envfrom the Cloudflare request environment, whichhappens after Worker module evaluation. So the Worker captured
undefinedfor the secret, base URL, and Google credentials, leaving auth non-functional in
production while working locally (where the env is present at import time).
The fix
buildAuthOptions(env = process.env)+getAuth()defer construction to thefirst request. Callers move off the
authsingleton:src/app/api/auth/[...all]/route.tssrc/app/api/checkout/route.tssrc/lib/auth-utils.tsAlso included
migrations/0002_better_auth_tables.sql— the D1 tables Better Auth needs(
user,session,account,verification). Singular names deliberatelykept separate from the app's existing plural
userstable.src/lib/auth.test.ts— covers the option builder against an injected env,which is what made the lazy split testable.
src/__tests__/user-menu.test.tsx— signed-out menu behaviour.callbackURLmoves/→/dashboard.🤖 Generated with Claude Code