[PR #7330] [CLOSED] feat :add afterCommit hook for user creation to prevent foreign key violations #24135

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7330
Author: @waliilaw
Created: 1/13/2026
Status: Closed

Base: canaryHead: fix/aftercommit-hook-7260


📝 Commits (2)

  • 483086c add afterCommit hook for user creation to prevent foreign key violations
  • 010947d fix: use request-scoped callbacks instead of shared state

📊 Changes

3 files changed (+83 additions, -13 deletions)

View changed files

📝 packages/better-auth/src/db/internal-adapter.ts (+30 -12)
📝 packages/better-auth/src/db/with-hooks.ts (+44 -1)
📝 packages/core/src/types/init-options.ts (+9 -0)

📄 Description

fixes #7260

adds a new databaseHooks.user.create.afterCommit hook that runs after the transaction commits, not inside it like the existing after hook.

problem

when using databaseHooks.user.create.after during oauth login, any code that tries to reference the newly created user (like creating related records) fails with foreign key constraint violations because the hook runs inside the transaction before it commits.

solution

  • added afterCommit hook type to user.create in init-options.ts
  • modified with-hooks.ts to collect afterCommit callbacks during transaction execution
  • added runAfterCommitCallbacks() and clearAfterCommitCallbacks() methods to internal adapter
  • updated createOAuthUser and sign-up route to execute afterCommit callbacks after transaction commits

usage

databaseHooks: {
  user: {
    create: {
      afterCommit: async (user, context) => {
        // this runs AFTER the transaction commits
        // safe to create records with foreign keys to user.id
        await db.insert(profiles).values({ userId: user.id, ... });
      }
    }
  }
}

<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Adds a user.create.afterCommit hook that runs after the transaction commits. Prevents foreign key violations during OAuth login and sign-up when creating related records. Fixes #7260.

- **New Features**
  - Adds databaseHooks.user.create.afterCommit for post-commit actions.
  - createWithHooks now returns { data, afterCommitCallbacks }; callbacks are request-scoped and executed after commit via executeAfterCommitCallbacks with per-callback error handling.
  - Internal adapter collects callbacks during OAuth user creation and runs them after commit; createUser and createAccount also run afterCommit callbacks outside the transaction.

- **Migration**
  - Move related-record creation from user.create.after to user.create.afterCommit.
  - No breaking changes; existing after still runs inside the transaction.

<sup>Written for commit 010947dfa49228fab133812c12190d59d35c69bf. Summary will update 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>
## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/7330 **Author:** [@waliilaw](https://github.com/waliilaw) **Created:** 1/13/2026 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `fix/aftercommit-hook-7260` --- ### 📝 Commits (2) - [`483086c`](https://github.com/better-auth/better-auth/commit/483086c8f680cc5167cd00cb07e059b0c9c64258) add afterCommit hook for user creation to prevent foreign key violations - [`010947d`](https://github.com/better-auth/better-auth/commit/010947dfa49228fab133812c12190d59d35c69bf) fix: use request-scoped callbacks instead of shared state ### 📊 Changes **3 files changed** (+83 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/internal-adapter.ts` (+30 -12) 📝 `packages/better-auth/src/db/with-hooks.ts` (+44 -1) 📝 `packages/core/src/types/init-options.ts` (+9 -0) </details> ### 📄 Description fixes #7260 adds a new `databaseHooks.user.create.afterCommit` hook that runs after the transaction commits, not inside it like the existing `after` hook. ### problem when using `databaseHooks.user.create.after` during oauth login, any code that tries to reference the newly created user (like creating related records) fails with foreign key constraint violations because the hook runs inside the transaction before it commits. ### solution - added `afterCommit` hook type to `user.create` in init-options.ts - modified with-hooks.ts to collect afterCommit callbacks during transaction execution - added `runAfterCommitCallbacks()` and `clearAfterCommitCallbacks()` methods to internal adapter - updated `createOAuthUser` and sign-up route to execute afterCommit callbacks after transaction commits ### usage ```typescript databaseHooks: { user: { create: { afterCommit: async (user, context) => { // this runs AFTER the transaction commits // safe to create records with foreign keys to user.id await db.insert(profiles).values({ userId: user.id, ... }); } } } } <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds a user.create.afterCommit hook that runs after the transaction commits. Prevents foreign key violations during OAuth login and sign-up when creating related records. Fixes #7260. - **New Features** - Adds databaseHooks.user.create.afterCommit for post-commit actions. - createWithHooks now returns { data, afterCommitCallbacks }; callbacks are request-scoped and executed after commit via executeAfterCommitCallbacks with per-callback error handling. - Internal adapter collects callbacks during OAuth user creation and runs them after commit; createUser and createAccount also run afterCommit callbacks outside the transaction. - **Migration** - Move related-record creation from user.create.after to user.create.afterCommit. - No breaking changes; existing after still runs inside the transaction. <sup>Written for commit 010947dfa49228fab133812c12190d59d35c69bf. Summary will update 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-15 22:11:40 -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#24135