Skip to content

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

@jd-solanki

Description

@jd-solanki

Hi 👋🏻

Description

I was facing issue similar to this when using better auth nuxt module.

Cloudflare Logs:

GET https://nuxthub-better-auth-repro.jdsolanki0001.workers.dev/ - Ok @ 25/3/2026, 12:04:21 am
GET https://nuxthub-better-auth-repro.jdsolanki0001.workers.dev/api/auth/get-session - Ok @ 25/3/2026, 12:04:22 am
  (error) 2026-03-24T18:34:22.461Z ERROR [Better Auth]: INTERNAL_SERVER_ERROR Error: Failed query: select "id", "expires_at", "token", "created_at", "updated_at", "ip_address", "user_agent", "user_id" from "session" where "session"."token" = $1
params: CfZ7HikYQH641Nxsp4nT6DyuMSb7NMTb

Previously I migrated my main project to NuxtHub + better-auth nuxt module but I was facing same error so I thought I might have misconfigured something because it was large project so I started from scratch and while I reached auth part I'm now facing same thing.

Reproduction

To reproduce this, I created:
Reproduction repo: https://github.com/jd-solanki/nuxthub-better-auth-repro

Steps:

  1. Fork, clone and ensure it runs fine locally
  2. Create DB on neon (or supabase or other)
  3. Create HyperDrive on CF and use direct connection string in setup
  4. pass/replace HyperDrive id in nuxt.config.ts (🚨🚨🚨 IMPORTANT)
  5. Ensure following env vars in CF build:
NUXT_BETTER_AUTH_SECRET=<random 32 character string>
DATABASE_URL=<direct connection string from neon/supabase so that migration applies>
  1. Deploy the project
  2. Run npx wrangler tail <worker-name> to see logs in terminal
  3. try /register, /login & /app page you may get error like mentioned above that it fails to query

Screenshot:
Image

Error:

POST https://nuxthub-better-auth-repro.jdsolanki0001.workers.dev/api/auth/sign-up/email - Ok @ 25/3/2026, 12:19:03 am
  (error) 2026-03-24T18:49:03.116Z ERROR [Better Auth]: Error Error: Failed query: select "id", "name", "email", "email_verified", "image", "created_at", "updated_at" from "user" where "user"."email" = $1
params: [email protected]
  (error) # SERVER_ERROR:  Error: Failed query: select "id", "name", "email", "email_verified", "image", "created_at", "updated_at" from "user" where "user"."email" = $1
params: [email protected]

Live demo: https://nuxthub-better-auth-repro.jdsolanki0001.workers.dev/login

to logout clean cookies ;)

Note

Sometime you may encounter error "Worker exceeded CPU time limit (503)" so kindly error that assuming it's not related to this issue but I'm not sure though. Have a look for 500 errors

Additional Context

I believe this is due to singleton of betterAuth and I'm not sure if this is handled in this module.

Reference: better-auth/better-auth#969 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions