[PR #5290] [MERGED] refactor: share generic context during api endpoint #5907

Closed
opened 2026-03-13 12:40:56 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5290
Author: @himself65
Created: 10/14/2025
Status: Merged
Merged: 10/15/2025
Merged by: @himself65

Base: canaryHead: himself65/2025/10/13/auth-context


📝 Commits (10+)

  • 88b8ba2 refactor: share generic context during api endpoint
  • e5f9b71 test: fix
  • 9f97346 test: fix
  • 2e6974f refactor: enhance context creation in auth endpoints
  • 1c21021 Revert "refactor: enhance context creation in auth endpoints"
  • 2ca9e64 refactor: enhance auth endpoint creation with context handling
  • e18d3c4 refactor: integrate endpoint context handling in session tests
  • bda6a1c refactor: subpath import
  • 9b17d25 refactor: update error message for missing auth context in endpoint callback
  • be8d5ce test: fix

📊 Changes

89 files changed (+736 additions, -887 deletions)

View changed files

📝 packages/better-auth/src/api/index.test.ts (+1 -1)
📝 packages/better-auth/src/api/index.ts (+1 -1)
📝 packages/better-auth/src/api/middlewares/origin-check.test.ts (+1 -1)
📝 packages/better-auth/src/api/middlewares/origin-check.ts (+1 -1)
📝 packages/better-auth/src/api/routes/account.ts (+10 -13)
📝 packages/better-auth/src/api/routes/callback.ts (+10 -13)
📝 packages/better-auth/src/api/routes/email-verification.ts (+1 -4)
📝 packages/better-auth/src/api/routes/error.ts (+1 -1)
📝 packages/better-auth/src/api/routes/ok.ts (+1 -1)
📝 packages/better-auth/src/api/routes/reset-password.ts (+18 -31)
📝 packages/better-auth/src/api/routes/session-api.test.ts (+63 -44)
📝 packages/better-auth/src/api/routes/session.ts (+1 -1)
📝 packages/better-auth/src/api/routes/sign-in.ts (+1 -2)
📝 packages/better-auth/src/api/routes/sign-out.ts (+1 -1)
📝 packages/better-auth/src/api/routes/sign-up.ts (+14 -21)
📝 packages/better-auth/src/api/routes/update-user.ts (+22 -31)
📝 packages/better-auth/src/api/to-auth-endpoints.test.ts (+1 -1)
📝 packages/better-auth/src/api/to-auth-endpoints.ts (+88 -86)
📝 packages/better-auth/src/auth.test.ts (+1 -1)
📝 packages/better-auth/src/call.test.ts (+1 -1)

...and 69 more files

📄 Description

Summary by cubic

Refactored endpoint execution to share a generic context via AsyncLocalStorage, removing the need to pass ctx into adapter and hook calls. This reduces boilerplate across routes/plugins and ensures consistent context during API handling.

  • Refactors

    • Added getEndpointContext and runWithEndpointContext in core.
    • Wrapped endpoint execution in toAuthEndpoints with runWithEndpointContext.
    • Removed context parameter from InternalAdapter methods and updated types.
    • Updated with-hooks utilities to read context via getEndpointContext.
    • Adjusted all routes, plugins, and tests to the new adapter signatures.
  • Migration

    • If you maintain a custom DB adapter, update method signatures to match the new InternalAdapter interface (no context arg).
    • If you call adapter/with-hooks outside of endpoint handlers, wrap execution with runWithEndpointContext(context, fn).

🔄 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/5290 **Author:** [@himself65](https://github.com/himself65) **Created:** 10/14/2025 **Status:** ✅ Merged **Merged:** 10/15/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `himself65/2025/10/13/auth-context` --- ### 📝 Commits (10+) - [`88b8ba2`](https://github.com/better-auth/better-auth/commit/88b8ba2e96074791518bea98fb1331673c4a490d) refactor: share generic context during api endpoint - [`e5f9b71`](https://github.com/better-auth/better-auth/commit/e5f9b71c965770efdef484df003f15d683804223) test: fix - [`9f97346`](https://github.com/better-auth/better-auth/commit/9f9734653dd3beb6097255e2ba89d53e09b5e661) test: fix - [`2e6974f`](https://github.com/better-auth/better-auth/commit/2e6974f1b600f668cb3eb5a4a34022905eaaa3b6) refactor: enhance context creation in auth endpoints - [`1c21021`](https://github.com/better-auth/better-auth/commit/1c210215414b81bf693b37adcae6a8fbe27a92fb) Revert "refactor: enhance context creation in auth endpoints" - [`2ca9e64`](https://github.com/better-auth/better-auth/commit/2ca9e64c5d08bb1e070bf414a9988ddcc7e34f85) refactor: enhance auth endpoint creation with context handling - [`e18d3c4`](https://github.com/better-auth/better-auth/commit/e18d3c42661e4192ff9640bee2c45272381582a3) refactor: integrate endpoint context handling in session tests - [`bda6a1c`](https://github.com/better-auth/better-auth/commit/bda6a1c5bd007df739ff6fc053c717d72f96f509) refactor: subpath import - [`9b17d25`](https://github.com/better-auth/better-auth/commit/9b17d2579002a3bad80c6486a24206ee5f6781e7) refactor: update error message for missing auth context in endpoint callback - [`be8d5ce`](https://github.com/better-auth/better-auth/commit/be8d5ce4e7021352377230a0ae514f3928cd518d) test: fix ### 📊 Changes **89 files changed** (+736 additions, -887 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/index.test.ts` (+1 -1) 📝 `packages/better-auth/src/api/index.ts` (+1 -1) 📝 `packages/better-auth/src/api/middlewares/origin-check.test.ts` (+1 -1) 📝 `packages/better-auth/src/api/middlewares/origin-check.ts` (+1 -1) 📝 `packages/better-auth/src/api/routes/account.ts` (+10 -13) 📝 `packages/better-auth/src/api/routes/callback.ts` (+10 -13) 📝 `packages/better-auth/src/api/routes/email-verification.ts` (+1 -4) 📝 `packages/better-auth/src/api/routes/error.ts` (+1 -1) 📝 `packages/better-auth/src/api/routes/ok.ts` (+1 -1) 📝 `packages/better-auth/src/api/routes/reset-password.ts` (+18 -31) 📝 `packages/better-auth/src/api/routes/session-api.test.ts` (+63 -44) 📝 `packages/better-auth/src/api/routes/session.ts` (+1 -1) 📝 `packages/better-auth/src/api/routes/sign-in.ts` (+1 -2) 📝 `packages/better-auth/src/api/routes/sign-out.ts` (+1 -1) 📝 `packages/better-auth/src/api/routes/sign-up.ts` (+14 -21) 📝 `packages/better-auth/src/api/routes/update-user.ts` (+22 -31) 📝 `packages/better-auth/src/api/to-auth-endpoints.test.ts` (+1 -1) 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+88 -86) 📝 `packages/better-auth/src/auth.test.ts` (+1 -1) 📝 `packages/better-auth/src/call.test.ts` (+1 -1) _...and 69 more files_ </details> ### 📄 Description <!-- This is an auto-generated description by cubic. --> ## Summary by cubic Refactored endpoint execution to share a generic context via AsyncLocalStorage, removing the need to pass ctx into adapter and hook calls. This reduces boilerplate across routes/plugins and ensures consistent context during API handling. - **Refactors** - Added getEndpointContext and runWithEndpointContext in core. - Wrapped endpoint execution in toAuthEndpoints with runWithEndpointContext. - Removed context parameter from InternalAdapter methods and updated types. - Updated with-hooks utilities to read context via getEndpointContext. - Adjusted all routes, plugins, and tests to the new adapter signatures. - **Migration** - If you maintain a custom DB adapter, update method signatures to match the new InternalAdapter interface (no context arg). - If you call adapter/with-hooks outside of endpoint handlers, wrap execution with runWithEndpointContext(context, fn). <!-- 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-03-13 12:40:56 -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#5907