[PR #5742] [MERGED] feat: add request state #31793

Closed
opened 2026-04-17 22:40:18 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5742
Author: @himself65
Created: 11/3/2025
Status: Merged
Merged: 11/5/2025
Merged by: @himself65

Base: canaryHead: himself65/2025/11/03/request-store


📝 Commits (9)

📊 Changes

16 files changed (+930 additions, -125 deletions)

View changed files

📝 docs/content/docs/concepts/oauth.mdx (+151 -0)
📝 packages/better-auth/package.json (+1 -0)
📝 packages/better-auth/src/api/middlewares/index.ts (+1 -0)
packages/better-auth/src/api/middlewares/oauth.ts (+36 -0)
📝 packages/better-auth/src/api/routes/account.ts (+12 -4)
📝 packages/better-auth/src/api/routes/sign-in.ts (+11 -1)
📝 packages/better-auth/src/api/to-auth-endpoints.ts (+116 -104)
📝 packages/better-auth/src/call.test.ts (+111 -2)
📝 packages/better-auth/src/oauth2/state.ts (+13 -1)
📝 packages/better-auth/src/plugins/generic-oauth/index.ts (+17 -5)
📝 packages/core/src/context/index.ts (+9 -0)
packages/core/src/context/request-state.test.ts (+346 -0)
packages/core/src/context/request-state.ts (+70 -0)
📝 packages/core/src/types/init-options.ts (+23 -0)
📝 packages/sso/src/index.ts (+1 -1)
📝 pnpm-lock.yaml (+12 -7)

📄 Description

Summary by cubic

Adds request-scoped state with AsyncLocalStorage to safely share schema-bound data across async boundaries, and runs auth endpoints within it or reuses an existing one. Enables validated OAuth additionalData to be passed through the flow and exposed to hooks via request state (not persisted).

  • New Features
    • RequestState using WeakMap<StandardSchemaV1, any>.
    • runWithRequestState(), getCurrentRequestState() (throws), hasRequestState(), getRequestStateAsyncLocalStorage().
    • defineRequestState(schema) for typed get/set.
    • Auth endpoints ensure a per-call request state and reuse when available.
    • OAuth middleware exposes getOauthState; set is internal to the flow.
    • OAuth: advanced.oauthConfig.additionalData { enabled?, schema?: StandardSchemaV1 } to validate and store data in request state.
    • signInSocial, generic OAuth, and linkSocialAccount accept additionalData; forwarded and available in hooks (not stored in DB).

Written for commit ae7fccda44. Summary will update automatically on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/5742 **Author:** [@himself65](https://github.com/himself65) **Created:** 11/3/2025 **Status:** ✅ Merged **Merged:** 11/5/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `himself65/2025/11/03/request-store` --- ### 📝 Commits (9) - [`7e834d3`](https://github.com/better-auth/better-auth/commit/7e834d3f5857907001f94d85bce3f21ba6905fbc) feat: request store - [`f166b99`](https://github.com/better-auth/better-auth/commit/f166b9951390d79ea859e7abcaa44625f453fb96) fix: code - [`94dd3e9`](https://github.com/better-auth/better-auth/commit/94dd3e9a5ad3cd98df6f8889716a5f886eb2f566) fix: code - [`be1d294`](https://github.com/better-auth/better-auth/commit/be1d2944c1867d28ba2eb2ceb12ac59ec5c575f7) fix: lint - [`538fab3`](https://github.com/better-auth/better-auth/commit/538fab383aafc61abd72756c71dbc4dd73715246) docs: update - [`3eca9d4`](https://github.com/better-auth/better-auth/commit/3eca9d4676c7789151eccc35a81afc1bc2a80c10) fix: test - [`3cbc32c`](https://github.com/better-auth/better-auth/commit/3cbc32c6d05868a070d22a4d3aee0bfadbf5722f) refactor: logic - [`f948ae7`](https://github.com/better-auth/better-auth/commit/f948ae75c854cb18b8691a1e187718cbaab1cf06) fix: request state - [`ae7fccd`](https://github.com/better-auth/better-auth/commit/ae7fccda44f2e8379f5c3e11cbd5779e5a39e783) fix: oauth store ### 📊 Changes **16 files changed** (+930 additions, -125 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/concepts/oauth.mdx` (+151 -0) 📝 `packages/better-auth/package.json` (+1 -0) 📝 `packages/better-auth/src/api/middlewares/index.ts` (+1 -0) ➕ `packages/better-auth/src/api/middlewares/oauth.ts` (+36 -0) 📝 `packages/better-auth/src/api/routes/account.ts` (+12 -4) 📝 `packages/better-auth/src/api/routes/sign-in.ts` (+11 -1) 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+116 -104) 📝 `packages/better-auth/src/call.test.ts` (+111 -2) 📝 `packages/better-auth/src/oauth2/state.ts` (+13 -1) 📝 `packages/better-auth/src/plugins/generic-oauth/index.ts` (+17 -5) 📝 `packages/core/src/context/index.ts` (+9 -0) ➕ `packages/core/src/context/request-state.test.ts` (+346 -0) ➕ `packages/core/src/context/request-state.ts` (+70 -0) 📝 `packages/core/src/types/init-options.ts` (+23 -0) 📝 `packages/sso/src/index.ts` (+1 -1) 📝 `pnpm-lock.yaml` (+12 -7) </details> ### 📄 Description <!-- This is an auto-generated description by cubic. --> ## Summary by cubic Adds request-scoped state with AsyncLocalStorage to safely share schema-bound data across async boundaries, and runs auth endpoints within it or reuses an existing one. Enables validated OAuth additionalData to be passed through the flow and exposed to hooks via request state (not persisted). - **New Features** - RequestState using WeakMap<StandardSchemaV1, any>. - runWithRequestState(), getCurrentRequestState() (throws), hasRequestState(), getRequestStateAsyncLocalStorage(). - defineRequestState(schema) for typed get/set. - Auth endpoints ensure a per-call request state and reuse when available. - OAuth middleware exposes getOauthState; set is internal to the flow. - OAuth: advanced.oauthConfig.additionalData { enabled?, schema?: StandardSchemaV1 } to validate and store data in request state. - signInSocial, generic OAuth, and linkSocialAccount accept additionalData; forwarded and available in hooks (not stored in DB). <sup>Written for commit ae7fccda44f2e8379f5c3e11cbd5779e5a39e783. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-17 22:40:18 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#31793