Skip to content

fix(auth): rebuild nuxthub adapters and server auth type refs#295

Open
onmax wants to merge 13 commits intomainfrom
fix/hyperdrive-auth-cache
Open

fix(auth): rebuild nuxthub adapters and server auth type refs#295
onmax wants to merge 13 commits intomainfrom
fix/hyperdrive-auth-cache

Conversation

@onmax
Copy link
Copy Markdown
Collaborator

@onmax onmax commented Mar 24, 2026

Closes #294
Fixes #309

Hyperdrive-backed NuxtHub PostgreSQL auth requests were reusing state across Worker requests in two places: the module-level Better Auth singleton and the generated Drizzle client for the NuxtHub PostgreSQL adapter. Direct @nuxthub/db access still worked, but Better Auth session or user reads could intermittently fail on deployed Workers.

This patch keeps the existing cache for non-database setups, rebuilds database-backed auth instances per request, and generates a fresh Hyperdrive postgres-js client with prepare: false for the NuxtHub PostgreSQL path.

It also fixes the #309 server auth config type regression while keeping closer to Nuxt’s type-context separation: server-auth inference templates are kept out of shared/root client-facing references, the circular @nuxthub/db augmentation is removed, and node project references derive filesystem aliases from Nuxt aliases instead of maintaining a hardcoded root/layer allowlist.

Checked locally:

pnpm test test/infer-plugins-types.test.ts test/schema-generator.test.ts test/server-auth-project-references-typecheck.test.ts
pnpm typecheck:runtime-server
pnpm lint

Verified in a fresh /tmp clone of jd-solanki/nuxthub-better-auth-repro/tree/alias-type-issue with:

pnpm add '@onmax/nuxt-better-auth@https://pkg.pr.new/nuxt-modules/better-auth/@onmax/nuxt-better-auth@e49d956'
pnpm install
pnpm exec nuxi prepare
pnpm exec vue-tsc --noEmit -p .nuxt/tsconfig.server.json --pretty false
NUXT_BETTER_AUTH_SECRET=0123456789abcdef0123456789abcdef pnpm run build

prepare and server typecheck pass. The production build gets past the previous ~~/*, layer alias, server auth context, and schema type errors; client build, server build, and prerender complete. It then exits while NuxtHub applies PostgreSQL migrations because the local repro has no real migration target.

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Mar 24, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
nuxt-better-auth-docs Ready Ready Preview, Comment Apr 26, 2026 9:40am

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Mar 24, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@onmax/nuxt-better-auth@295

commit: e49d956

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Mar 24, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
❌ Deployment failed
View logs
better-auth e49d956 Apr 26 2026, 09:40 AM

@onmax onmax force-pushed the fix/hyperdrive-auth-cache branch 2 times, most recently from e752578 to c2e43a6 Compare March 24, 2026 20:08
@jd-solanki
Copy link
Copy Markdown
Contributor

jd-solanki commented Mar 25, 2026

Hey @onmax

Here's few issues I found:

  1. queries are still failing when using database hooks (which means it may happen without this hooks but this errors are occasional I believe this is related to point 2 I mentioned below):
    From repro:

    POST https://nuxthub-better-auth-repro.jdsolanki0001.workers.dev/api/auth/sign-in/email - Ok @ 3/25/2026, 12:52:43 PM
      (error) [request error] [unhandled] [POST] https://nuxthub-better-auth-repro.jdsolanki0001.workers.dev/api/auth/sign-in/email
     {
      message: 'Failed query: select "id", "expires_at", "token", "created_at", "updated_at", "ip_address", "user_agent", "user_id" from "session" limit $1\n' +
        'params: 1',
      statusCode: 500
    }
    Screenshot image
  2. It seems our code is not optimised enough because we're getting Error: Worker exceeded CPU time limit error. Try login multiple times and you will receive 503 error in network tab and you may get full page error as attached below

    page screenshot image

    From my actual app:

    POST https://nuxtstart.jdsolanki0001.workers.dev/api/auth/sign-in/email - Ok @ 25/3/2026, 12:23:18 pm
      (error) [request error] [unhandled] [POST] https://nuxtstart.jdsolanki0001.workers.dev/api/auth/sign-in/email
     {
      message: 'Failed query: update "user" set "updated_at" = $1, "last_sign_in_at" = $2 where "user"."id" = $3\n' +
        'params: 2026-03-25T06:53:20.965Z,2026-03-25T06:53:20.965Z,NaRranaO7OEatauhsvvvN8ZLKXXfNqHa',
      statusCode: 500
    }
  3. Tried pnpm i https://pkg.pr.new/@onmax/nuxt-better-auth@295 in my other repo which uses nuxt layers but it seems pkg pr is outdated because it isn't able to identify auth config I have in one of my layers.

    ERROR  [nuxt-better-auth] Missing server/auth.config.ts - create with defineServerAuth()

Hence using github:nuxt-modules/better-auth#pull/295/head for now


related: cloudflare/workers-sdk#6179

@onmax
Copy link
Copy Markdown
Collaborator Author

onmax commented Mar 26, 2026

@jd-solanki Please try again when you have a moment. I’ve pushed a fix for the issue on our side.

@jd-solanki
Copy link
Copy Markdown
Contributor

jd-solanki commented Mar 27, 2026

Hey @onmax

sorry I was not able to test it yesterday.

I tried latest commit and now we have initial installation issue where we first install the packages:


image

Even after this error when I run dev command we get error in browser:

Cannot find package 'nitropack' imported from /private/tmp/nuxthub-better-auth/.nuxt/better-auth/database.mjs
screenshot image

I also check installed modules just to be sure:
image

NOTE: I pushed latest commit of this PR to my repro if you want to try it locally: https://github.com/jd-solanki/nuxthub-better-auth-repro?rgh-link-date=2026-03-24T18%3A52%3A51.000Z

@onmax
Copy link
Copy Markdown
Collaborator Author

onmax commented Mar 27, 2026

@jd-solanki I pushed another fix and the fresh pkg.pr.new package is ready here:

https://pkg.pr.new/nuxt-modules/better-auth/@onmax/nuxt-better-auth@9a4ad00

Please use this owner/repo-qualified URL instead of the compact @295 alias. I verified that this long URL resolves to the current package build for this PR.

I tested it against your repro locally and I could no longer reproduce the nitropack import error. Could you try again and let me know if pnpm install or pnpm dev still fails, ideally with the exact new error output?

@jd-solanki
Copy link
Copy Markdown
Contributor

jd-solanki commented Mar 29, 2026

Hey @onmax

I tried latest commit but still facing following:

  • ni hung up
  • If I kill (ctrl + c) the ni hung up and run nr dev then end up with Cannot find package 'nitropack' imported from /private/tmp/nuxthub-better-auth-repro/.nuxt/better-auth/database.mjs
  • Same SQL query failures

I updated package lock to exact commit this time so now if you pull latest changes and run ni you might face the same error.

BTW it's surpring to me that your ni didn't froze 🤔

I request you to also validate when you have in your node_modules dir because for me pkg.pr was not installing latest changes you make in this PR.

For example, with pkg.pr command ni https://pkg.pr.new/@onmax/nuxt-better-auth@29, node_modules/@onmax/nuxt-better-auth/dist/runtime/server/utils/auth.js path don't have:

import { useRuntimeConfig } from "nitropack/runtime";

instead of

import { useRuntimeConfig } from "#imports";

@onmax
Copy link
Copy Markdown
Collaborator Author

onmax commented Mar 29, 2026

I retried this from a fresh /tmp repro and found that the compact pkg.pr.new alias can be stale for this package.

Please avoid the github:... install for this check. With pnpm 10, git-hosted packages can fail because they need a prepare step.

Please also use the owner/repo-qualified pkg.pr.new URL below instead of the compact @295 alias:

rm -rf node_modules .nuxt
pnpm install https://pkg.pr.new/nuxt-modules/better-auth/@onmax/nuxt-better-auth@9a4ad00
pnpm dev

I verified this long URL resolves to the current package build for this PR.

If it still fails, please paste:

  • the exact install command you used
  • the full new error output

@jd-solanki
Copy link
Copy Markdown
Contributor

jd-solanki commented Apr 11, 2026

Steps:

  1. Made a fresh clone
  2. Ran pnpm install https://pkg.pr.new/@onmax/nuxt-better-auth@295
  3. Ran nr dev
  4. Opened localhost:3000 in browser
  5. Error
    Cannot find module '/private/tmp/nuxthub-better-auth-repro-1/.nuxt/hub/db.mjs' imported from /private/tmp/nuxthub-better-auth-repro-1/.nuxt/better-auth/database.mjs
    
Screenshot localhost_3000_

node_modules/@onmax dir created by install command in step 2:
@onmax.zip

@jd-solanki
Copy link
Copy Markdown
Contributor

jd-solanki commented Apr 12, 2026

With new changes nuxt prepare command fails, cloudflare build fails but works with node-server runtime:

14:43:04.640	[error] Failed to load auth config: Package subpath './runtime/context' is not defined by "exports" in /opt/buildhome/repo/node_modules/.pnpm/node_modules/nitropack/package.json
14:43:04.640	  at loadUserAuthConfig (node_modules/.pnpm/@onmax+nuxt-better-auth@https+++pkg.pr.new+nuxt-modules+better-auth+@onmax+nuxt-better-auth@9_3y4kncq5g2trjbycuwre5utoxe/node_modules/@onmax/nuxt-better-auth/dist/module.mjs:327:13)
14:43:04.640	  at async loadAuthOptions (node_modules/.pnpm/@onmax+nuxt-better-auth@https+++pkg.pr.new+nuxt-modules+better-auth+@onmax+nuxt-better-auth@9_3y4kncq5g2trjbycuwre5utoxe/node_modules/@onmax/nuxt-better-auth/dist/module.mjs:376:22)
14:43:04.640	  at async setupBetterAuthSchema (node_modules/.pnpm/@onmax+nuxt-better-auth@https+++pkg.pr.new+nuxt-modules+better-auth+@onmax+nuxt-better-auth@9_3y4kncq5g2trjbycuwre5utoxe/node_modules/@onmax/nuxt-better-auth/dist/module.mjs:391:37)
14:43:04.640	  at async setup (node_modules/.pnpm/@onmax+nuxt-better-auth@https+++pkg.pr.new+nuxt-modules+better-auth+@onmax+nuxt-better-auth@9_3y4kncq5g2trjbycuwre5utoxe/node_modules/@onmax/nuxt-better-auth/dist/module.mjs:1102:9)
14:43:04.642	  at async normalizedModule (node_modules/.pnpm/@nuxt+kit@4.4.2_magicast@0.5.2/node_modules/@nuxt/kit/dist/index.mjs:224:10)
14:43:04.643	  at async callModule (node_modules/.pnpm/@nuxt+kit@4.4.2_magicast@0.5.2/node_modules/@nuxt/kit/dist/index.mjs:848:46)
14:43:04.643	  at async installModules (node_modules/.pnpm/@nuxt+kit@4.4.2_magicast@0.5.2/node_modules/@nuxt/kit/dist/index.mjs:688:3)
14:43:04.643	  at async initNuxt (node_modules/.pnpm/nuxt@4.4.2_xx4epqdvw2sfshelqhtg5nqilq/node_modules/nuxt/dist/index.mjs:7151:3)
14:43:04.643	  at async loadNuxt (node_modules/.pnpm/nuxt@4.4.2_xx4epqdvw2sfshelqhtg5nqilq/node_modules/nuxt/dist/index.mjs:7385:5)
14:43:04.643	  at async loadNuxt (node_modules/.pnpm/@nuxt+kit@4.4.2_magicast@0.5.2/node_modules/@nuxt/kit/dist/index.mjs:1050:9)
14:43:04.643	  at async Object.run (node_modules/.pnpm/@nuxt+cli@3.34.0_@nuxt+schema@4.4.2_cac@6.7.14_magicast@0.5.2/node_modules/@nuxt/cli/dist/prepare-etx17Cow.mjs:30:16)
14:43:04.643	  at async runCommand (node_modules/.pnpm/citty@0.2.1/node_modules/citty/dist/index.mjs:188:47)
14:43:04.643	  at async runCommand (node_modules/.pnpm/citty@0.2.1/node_modules/citty/dist/index.mjs:185:21)
14:43:04.643	  at async runMain (node_modules/.pnpm/citty@0.2.1/node_modules/citty/dist/index.mjs:285:10) 
14:43:04.644	
14:43:05.150	 ELIFECYCLE  Command failed with exit code 1.
14:43:05.435	Failed: error occurred while installing tools or dependencies

Note

I updated the repro with your NuxtHub PR and betterAuth pkg switched to pkg.pr

@jd-solanki
Copy link
Copy Markdown
Contributor

jd-solanki commented Apr 23, 2026

Hey @onmax

I tried latest PR commit in repro and now getting that old error:

Cannot find package 'nitropack' imported from /Users/jd/Projects/mine/nuxthub-better-auth-repro/.nuxt/better-auth/database.mjs

@onmax
Copy link
Copy Markdown
Collaborator Author

onmax commented Apr 23, 2026

yeah sorry, i was busy and forgot to push the fix!

@jd-solanki
Copy link
Copy Markdown
Contributor

jd-solanki commented Apr 23, 2026

Sorry, I thought you're done with the PR and waiting for me.

Still 😪

Error: Package import specifier "#imports" is not defined in package /Users/jd/Projects/mine/nuxthub-better-auth-repro/package.json imported from /Users/jd/Projects/mine/nuxthub-better-auth-repro/.nuxt/better-auth/database.mjs

Stack trace:
  at importNotDefined (node:internal/modules/esm/resolve:300:10)
  at packageImportsResolve (node:internal/modules/esm/resolve:750:9)
  at moduleResolve (node:internal/modules/esm/resolve:843:16)
  at defaultResolve (node:internal/modules/esm/resolve:983:11)
  at #cachedDefaultResolve (node:internal/modules/esm/loader:717:20)
  at ModuleLoader.resolve (node:internal/modules/esm/loader:694:38)
  at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:308:38)
  at ModuleJob._link (node:internal/modules/esm/module_job:183:49)
  at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

@onmax
Copy link
Copy Markdown
Collaborator Author

onmax commented Apr 23, 2026

hey @jd-solanki, thanks for the patience! I did fix the bug and it seems working on my machine now again. I pushed the fix in this pr

@onmax onmax force-pushed the fix/hyperdrive-auth-cache branch from e5f9ec4 to 2e58e5d Compare April 26, 2026 07:19
@onmax onmax changed the title fix(auth): rebuild nuxthub postgres adapters per request fix(auth): rebuild nuxthub adapters and server auth type refs Apr 26, 2026
@onmax onmax force-pushed the fix/hyperdrive-auth-cache branch from 58b8aaa to cc0b0fa Compare April 26, 2026 08:33
@onmax onmax force-pushed the fix/hyperdrive-auth-cache branch from cc0b0fa to 36fa3cc Compare April 26, 2026 08:38
@onmax onmax force-pushed the fix/hyperdrive-auth-cache branch from 36fa3cc to 7575728 Compare April 26, 2026 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants