Skip to content

fix(schema): skip generation when the auth config fails to load - #422

Open
jd-solanki wants to merge 1 commit into
nuxt-modules:mainfrom
jd-solanki:fix/no-schema-write-on-config-load-failure
Open

fix(schema): skip generation when the auth config fails to load#422
jd-solanki wants to merge 1 commit into
nuxt-modules:mainfrom
jd-solanki:fix/no-schema-write-on-config-load-failure

Conversation

@jd-solanki

Copy link
Copy Markdown
Contributor

Why

Reported in #419. When server/auth.config.ts fails to load, nuxt dev logs an error and then
generates the Drizzle schema anyway, from an empty options object. The correct
.nuxt/better-auth/schema.<dialect>.ts already on disk is replaced by one carrying core tables
only, losing every user.additionalFields entry and every plugin-contributed column.

drizzle-kit generate reads that file next, so the migration it produces drops those columns.
The dev server keeps serving normally and the success log line reads the same as a healthy run,
so nothing signals that the schema is now wrong. There is a runnable reproduction on the issue.

Notes

loadUserAuthConfig's return type gains | null. Despite the export, it is unreachable from
any consumer: build.config.ts builds only the module entry, the exports map exposes just .,
./composables and ./config, and src/module.ts does not re-export it. The only call site is
in src/module/schema.ts. A non-enumerable marker on the existing {} would have been strictly
additive, but null lets the type checker find the call sites, which seemed the better trade for
a bug fix. Say the word if you would rather have the additive version.

The hub:db:schema:extend registration moved above the try deliberately. Left where it was, the
new early return skips it, and NuxtHub ends up with no auth schema path at all: the correct file
preserved on disk but orphaned from its only consumer. setupBetterAuthSchema runs once at module
setup, so there is no second chance. There is a regression test for this.

better-auth:config:extend no longer fires when the load failed, since loadAuthOptions returns
before it. Nothing consumes its result on that path, but it is a visible change if a module relies
on the hook firing on every setup. Moving the bail below the hook is a one-line change if you
prefer that.

nuxt dev still does not throw the way nuxt prepare does. That is deliberate. It is a behaviour
change that did not belong in a bug fix, and the reported problem is fixed without it.

Unrelated and untouched: setupBetterAuthSchema's catch ends with if (isProduction) throw error
followed by an unconditional throw error, so the production check is dead. Worth its own issue.

Closes #419

`loadUserAuthConfig` swallowed a load failure in dev and returned `{}`,
indistinguishable from an empty-but-valid config. `setupBetterAuthSchema`
then generated a schema from those empty options and overwrote
`.nuxt/better-auth/schema.<dialect>.ts` with core tables only, dropping
every `user.additionalFields` entry and every plugin-contributed column.
The next `drizzle-kit generate` read that file and emitted a migration
dropping those columns. `nuxt prepare` throws on the same failure, so only
the dev server was affected — and it kept serving with no sign that the
schema on disk was now wrong.

`loadUserAuthConfig` now returns `null` on a swallowed failure, from both
the thrown-error `catch` and the "does not export default" branch, so
callers can tell failure from an empty config. `setupBetterAuthSchema`
returns before touching the filesystem, leaving the previously generated
file alone, and the dev-mode error says so rather than the old, now
untrue "Schema may be incomplete".

Registration of the `hub:db:schema:extend` hook moves above the bail: the
whole point is to keep the good file on disk, so NuxtHub must still resolve
it on a run that generates nothing. `resolveHubSchemaPath` is a plain
filesystem lookup and does not care which run produced the file.

Behaviour on a successful load is unchanged, and dev still does not throw.

Closes nuxt-modules#419

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mn1xnJsSCpdbPEEZAFL1Si
@vercel

vercel Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

@jd-solanki is attempting to deploy a commit to the maximogarciamartinez's projects Team on Vercel.

A member of the Team first needs to authorize it.

@pkg-pr-new

pkg-pr-new Bot commented Aug 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nuxtjs/better-auth@422

commit: c374fe9

@jd-solanki
jd-solanki marked this pull request as ready for review August 28, 2026 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: dev overwrites a good schema when the auth config fails to load

1 participant