Skip to content

fix(auth): rebuild nuxthub postgres adapters per request#295

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

fix(auth): rebuild nuxthub postgres adapters per request#295
onmax wants to merge 5 commits intomainfrom
fix/hyperdrive-auth-cache

Conversation

@onmax
Copy link
Copy Markdown
Collaborator

@onmax onmax commented Mar 24, 2026

Closes #294

Summary

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, but rebuilds database-backed auth instances per request and generates a fresh Hyperdrive postgres-js client with prepare: false for the NuxtHub PostgreSQL path.

StackBlitz

Link Expected
Bug nuxt-better-auth-294 ✅ Builds; deployed Hyperdrive auth reads can fail
Fix nuxt-better-auth-294-fix ✅ Builds; deployed Hyperdrive auth reads stay stable

CLI Reproduction

git clone --depth 1 --filter=blob:none --sparse https://github.com/onmax/repros.git
cd repros && git sparse-checkout set nuxt-better-auth-294
cd nuxt-better-auth-294 && pnpm i && pnpm build

The runtime bug requires a real Cloudflare Hyperdrive binding. After deploy, verify the bug repro by hitting /api/db-check, signing up, and then repeating authenticated /api/auth-check requests.

Verify fix

git sparse-checkout add nuxt-better-auth-294-fix
cd ../nuxt-better-auth-294-fix && pnpm i && pnpm build

@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 Mar 27, 2026 7:27am

@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: 5ea1fdd

@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 5ea1fdd Mar 27 2026, 07:27 AM

@onmax onmax force-pushed the fix/hyperdrive-auth-cache branch from e782574 to e752578 Compare March 24, 2026 20:06
@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 is ready: https://pkg.pr.new/@onmax/nuxt-better-auth@295

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 I could not reproduce the nitropack error on my side when using pkg.pr.new.

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

Could you try again with a clean reset and the pkg.pr.new package first?

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

If it still fails, please paste:

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

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.

BetterAuth can't query user/session when cloudflare hyperdrive is used

2 participants