[PR #6713] [MERGED] feat: add a global backgroundTasks config option to defer actions like sending email and updates to run after response is sent #32445

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6713
Author: @nexxeln
Created: 12/12/2025
Status: Merged
Merged: 12/20/2025
Merged by: @Bekacru

Base: canaryHead: feat/api-key-defer-updates


📝 Commits (10+)

  • 494173f feat(api-key): add deferUpdates option
  • 917904e refactor(api-key): use global backgroundTasks config for deferUpdates
  • c9d39ac Update docs/content/docs/plugins/api-key.mdx
  • 011d61b Update docs/content/docs/plugins/api-key.mdx
  • 46ae12c Merge branch 'canary' into feat/api-key-defer-updates
  • d3a6ac7 refactor: simplify runInBackground with default handler
  • a642f82 add runInBgOrAwait
  • 74056a0 remove catches
  • 6504bfe Merge branch 'canary' into feat/api-key-defer-updates
  • 07379d3 refactor(auth): streamline email verification handling and improve background task error logging

📊 Changes

23 files changed (+623 additions, -274 deletions)

View changed files

📝 docs/content/docs/plugins/api-key.mdx (+53 -0)
📝 packages/better-auth/src/api/routes/email-verification.ts (+36 -26)
📝 packages/better-auth/src/api/routes/reset-password.ts (+4 -6)
📝 packages/better-auth/src/api/routes/sign-in.ts (+9 -7)
📝 packages/better-auth/src/api/routes/sign-up.test.ts (+1 -0)
📝 packages/better-auth/src/api/routes/sign-up.ts (+6 -18)
📝 packages/better-auth/src/api/routes/update-user.ts (+41 -33)
📝 packages/better-auth/src/context/__snapshots__/create-context.test.ts.snap (+2 -0)
📝 packages/better-auth/src/context/create-context.ts (+24 -0)
📝 packages/better-auth/src/oauth2/link-account.ts (+11 -8)
📝 packages/better-auth/src/plugins/api-key/api-key.test.ts (+158 -0)
📝 packages/better-auth/src/plugins/api-key/index.ts (+12 -7)
📝 packages/better-auth/src/plugins/api-key/routes/index.ts (+1 -0)
📝 packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts (+90 -83)
📝 packages/better-auth/src/plugins/api-key/types.ts (+15 -0)
📝 packages/better-auth/src/plugins/email-otp/index.ts (+21 -17)
📝 packages/better-auth/src/plugins/email-otp/routes.ts (+13 -13)
📝 packages/better-auth/src/plugins/organization/routes/crud-invites.ts (+36 -28)
📝 packages/better-auth/src/plugins/phone-number/routes.ts (+30 -20)
📝 packages/better-auth/src/plugins/two-factor/otp/index.ts (+8 -1)

...and 3 more files

📄 Description

Adds a global backgroundTasks config option and deferUpdates option to the API key plugin that allows deferring non-critical database updates to run after the response is sent.

Changes

  • Added backgroundTasks.handler to main auth options for scheduling background tasks
  • Added deferUpdates option to API key plugin to opt-in to deferred updates
  • Rate limit counters, timestamps, and remaining count updates can now be deferred
  • Rate limit validation still happens synchronously before deferring updates
  • Added tests for deferred updates functionality
  • Updated documentation with usage examples for Vercel and Cloudflare Workers

🔄 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/6713 **Author:** [@nexxeln](https://github.com/nexxeln) **Created:** 12/12/2025 **Status:** ✅ Merged **Merged:** 12/20/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `feat/api-key-defer-updates` --- ### 📝 Commits (10+) - [`494173f`](https://github.com/better-auth/better-auth/commit/494173fd9fc6ae5cca215e59109b92823b491081) feat(api-key): add deferUpdates option - [`917904e`](https://github.com/better-auth/better-auth/commit/917904e5d1005cda634ed55d3ceba0005c3ad325) refactor(api-key): use global backgroundTasks config for deferUpdates - [`c9d39ac`](https://github.com/better-auth/better-auth/commit/c9d39acd78c6c2e0cf9c45e98ace7230144eff39) Update docs/content/docs/plugins/api-key.mdx - [`011d61b`](https://github.com/better-auth/better-auth/commit/011d61b8252faf609f7811019745b1877be42290) Update docs/content/docs/plugins/api-key.mdx - [`46ae12c`](https://github.com/better-auth/better-auth/commit/46ae12ca67cd3a7cf488f7236984f1de66409317) Merge branch 'canary' into feat/api-key-defer-updates - [`d3a6ac7`](https://github.com/better-auth/better-auth/commit/d3a6ac78d7997e6fff7033ceb0003361bb032d3e) refactor: simplify runInBackground with default handler - [`a642f82`](https://github.com/better-auth/better-auth/commit/a642f82a6609144be9592be94c8f7f4ebdb73771) add runInBgOrAwait - [`74056a0`](https://github.com/better-auth/better-auth/commit/74056a0aa5dffe3551d2661ad983520ea516ea38) remove catches - [`6504bfe`](https://github.com/better-auth/better-auth/commit/6504bfed82bb03bc8e0cd854f03e62a156f9ecbf) Merge branch 'canary' into feat/api-key-defer-updates - [`07379d3`](https://github.com/better-auth/better-auth/commit/07379d3cdbc45c0cea12aa9d46d901af87033611) refactor(auth): streamline email verification handling and improve background task error logging ### 📊 Changes **23 files changed** (+623 additions, -274 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/api-key.mdx` (+53 -0) 📝 `packages/better-auth/src/api/routes/email-verification.ts` (+36 -26) 📝 `packages/better-auth/src/api/routes/reset-password.ts` (+4 -6) 📝 `packages/better-auth/src/api/routes/sign-in.ts` (+9 -7) 📝 `packages/better-auth/src/api/routes/sign-up.test.ts` (+1 -0) 📝 `packages/better-auth/src/api/routes/sign-up.ts` (+6 -18) 📝 `packages/better-auth/src/api/routes/update-user.ts` (+41 -33) 📝 `packages/better-auth/src/context/__snapshots__/create-context.test.ts.snap` (+2 -0) 📝 `packages/better-auth/src/context/create-context.ts` (+24 -0) 📝 `packages/better-auth/src/oauth2/link-account.ts` (+11 -8) 📝 `packages/better-auth/src/plugins/api-key/api-key.test.ts` (+158 -0) 📝 `packages/better-auth/src/plugins/api-key/index.ts` (+12 -7) 📝 `packages/better-auth/src/plugins/api-key/routes/index.ts` (+1 -0) 📝 `packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts` (+90 -83) 📝 `packages/better-auth/src/plugins/api-key/types.ts` (+15 -0) 📝 `packages/better-auth/src/plugins/email-otp/index.ts` (+21 -17) 📝 `packages/better-auth/src/plugins/email-otp/routes.ts` (+13 -13) 📝 `packages/better-auth/src/plugins/organization/routes/crud-invites.ts` (+36 -28) 📝 `packages/better-auth/src/plugins/phone-number/routes.ts` (+30 -20) 📝 `packages/better-auth/src/plugins/two-factor/otp/index.ts` (+8 -1) _...and 3 more files_ </details> ### 📄 Description Adds a global `backgroundTasks` config option and `deferUpdates` option to the API key plugin that allows deferring non-critical database updates to run after the response is sent. ## Changes - Added `backgroundTasks.handler` to main auth options for scheduling background tasks - Added `deferUpdates` option to API key plugin to opt-in to deferred updates - Rate limit counters, timestamps, and remaining count updates can now be deferred - Rate limit validation still happens synchronously before deferring updates - Added tests for deferred updates functionality - Updated documentation with usage examples for Vercel and Cloudflare Workers --- <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 23:15: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#32445