Skip to content

chore(core): use global for multiple instances of BetterAuth#7334

Merged
himself65 merged 3 commits intobetter-auth:canaryfrom
himself65:himself65/2026/01/13/global
Jan 13, 2026
Merged

chore(core): use global for multiple instances of BetterAuth#7334
himself65 merged 3 commits intobetter-auth:canaryfrom
himself65:himself65/2026/01/13/global

Conversation

@himself65
Copy link
Copy Markdown
Contributor

@himself65 himself65 commented Jan 13, 2026

Fixes: #7023 (comment)


Summary by cubic

Share AsyncLocalStorage via a process-global BetterAuth registry so multiple BetterAuth instances in the same process use the same contexts. Fixes context/transaction issues caused by duplicate packages under SSR/bundlers.

  • Refactors
    • Added getBetterAuthGlobal (global.ts) to store ALS instances in globalThis with a debug epoch counter.
    • endpoint-context, request-state, and transaction now read/write ALS from the shared global context.
    • Removed module-level symbols and local caches to avoid duplicate storages.
    • No API changes. Consistent request state and transactions across instances.

Written for commit 481e472. Summary will update on new commits.

@himself65 himself65 requested a review from Bekacru as a code owner January 13, 2026 17:26
Copilot AI review requested due to automatic review settings January 13, 2026 17:26
@vercel
Copy link
Copy Markdown

vercel bot commented Jan 13, 2026

@himself65 is attempting to deploy a commit to the better-auth Team on Vercel.

A member of the Team first needs to authorize it.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Jan 13, 2026

Open in StackBlitz

better-auth

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

@better-auth/cli

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/cli@7334

@better-auth/core

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/core@7334

@better-auth/expo

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/expo@7334

@better-auth/oauth-provider

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/oauth-provider@7334

@better-auth/passkey

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/passkey@7334

@better-auth/scim

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/scim@7334

@better-auth/sso

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/sso@7334

@better-auth/stripe

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/stripe@7334

@better-auth/telemetry

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/telemetry@7334

commit: 481e472

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 4 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="packages/core/src/context/global.ts">

<violation number="1" location="packages/core/src/context/global.ts:31">
P2: `epoch` is incremented the first time this module calls `getBetterAuthGlobal()` because `bind` is never set when the global object is created. The next call sees `bind` still null and increments `epoch`, so a single instance is reported as two. Assign `bind` during initialization so the counter only increments when a separate module copy loads.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

himself65 and others added 2 commits January 13, 2026 09:35
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
@himself65 himself65 merged commit e496c39 into better-auth:canary Jan 13, 2026
9 of 11 checks passed
himself65 added a commit to himself65/better-auth that referenced this pull request Jan 13, 2026
…auth#7334)

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
himself65 added a commit that referenced this pull request Jan 14, 2026
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
himself65 added a commit that referenced this pull request Jan 15, 2026
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
himself65 added a commit that referenced this pull request Jan 28, 2026
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
rae-fcm pushed a commit to FullCodeMedical/better-auth that referenced this pull request Mar 9, 2026
…auth#7334)

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
@himself65 himself65 review requested due to automatic review settings March 24, 2026 01:50
@github-actions
Copy link
Copy Markdown
Contributor

This pull request has been locked as it was closed more than 7 days ago. If these changes are still relevant or you have additional context, please open a new PR and reference this one.

@github-actions github-actions bot added the locked Locked conversations after being closed for 7 days label Mar 31, 2026
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 31, 2026
@gustavovalverde gustavovalverde added the core Core infra, API routes, session, cookies, client SDK label Apr 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

core Core infra, API routes, session, cookies, client SDK locked Locked conversations after being closed for 7 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

State mismatch error when signing in with social providers in Next.js integration since v1.4.4

2 participants