[PR #4215] [CLOSED] fix(api): rollback upon failure to create account/session #30858

Closed
opened 2026-04-17 21:52:06 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4215
Author: @ShobhitPatra
Created: 8/25/2025
Status: Closed

Base: canaryHead: fix/api


📝 Commits (5)

  • 8bf8a0c fix(api): rollback upon failure to create account/session after user creation
  • 4f607ec chore(api): add rollback test for failed session creation after user signup
  • 213bfb7 chore(api): fix linting issues
  • c58769c Merge branch 'canary' into fix/api
  • d662956 fix: use transaction

📊 Changes

2 files changed (+190 additions, -153 deletions)

View changed files

📝 packages/better-auth/src/api/routes/sign-up.test.ts (+26 -0)
📝 packages/better-auth/src/api/routes/sign-up.ts (+164 -153)

📄 Description

Summary

Fixes #4193 - Ensures proper rollback when user creation succeeds but subsequent account or session creation fails, preventing orphaned user records.

Problem

Previously, when user creation succeeded but account/session creation failed afterward, the user record would remain in the database without proper cleanup, leading to -Orphaned user records without associated accounts/sessions

Solution

Wrapped account linking and session creation in comprehensive try-catch block
Implemented rollback mechanism that attempts to delete the created user if subsequent operations fail


Summary by cubic

Prevents orphaned users by rolling back sign-up if account linking, email verification, or session creation fails. Fixes #4193.

  • Bug Fixes
    • Wrapped account linking, email verification, and session creation in a try/catch.
    • On failure, delete the user when supported; otherwise remove sessions and accounts and log warnings to avoid orphans.
    • Early return when autoSignIn is disabled or email verification is required (no session/token).
    • Set session cookie only after successful session creation and propagate original errors.

🔄 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/4215 **Author:** [@ShobhitPatra](https://github.com/ShobhitPatra) **Created:** 8/25/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `fix/api` --- ### 📝 Commits (5) - [`8bf8a0c`](https://github.com/better-auth/better-auth/commit/8bf8a0c990f38d2730b95e974c9165752e9614f8) fix(api): rollback upon failure to create account/session after user creation - [`4f607ec`](https://github.com/better-auth/better-auth/commit/4f607ec22fc4e94160843fefb1dd147499838f1c) chore(api): add rollback test for failed session creation after user signup - [`213bfb7`](https://github.com/better-auth/better-auth/commit/213bfb70d30e4d6d924e7d13f4d2f90161ce40c9) chore(api): fix linting issues - [`c58769c`](https://github.com/better-auth/better-auth/commit/c58769c35a4be40b80a87cc611c222a8aa4ff064) Merge branch 'canary' into fix/api - [`d662956`](https://github.com/better-auth/better-auth/commit/d6629564de3a5faf22f4c76eaaee27bb0aba9999) fix: use `transaction` ### 📊 Changes **2 files changed** (+190 additions, -153 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/routes/sign-up.test.ts` (+26 -0) 📝 `packages/better-auth/src/api/routes/sign-up.ts` (+164 -153) </details> ### 📄 Description ### Summary Fixes #4193 - Ensures proper rollback when user creation succeeds but subsequent account or session creation fails, preventing orphaned user records. ### Problem Previously, when user creation succeeded but account/session creation failed afterward, the user record would remain in the database without proper cleanup, leading to -Orphaned user records without associated accounts/sessions ### Solution Wrapped account linking and session creation in comprehensive try-catch block Implemented rollback mechanism that attempts to delete the created user if subsequent operations fail <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Prevents orphaned users by rolling back sign-up if account linking, email verification, or session creation fails. Fixes #4193. - **Bug Fixes** - Wrapped account linking, email verification, and session creation in a try/catch. - On failure, delete the user when supported; otherwise remove sessions and accounts and log warnings to avoid orphans. - Early return when autoSignIn is disabled or email verification is required (no session/token). - Set session cookie only after successful session creation and propagate original errors. <!-- 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 21:52:06 -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#30858