[PR #9193] fix(org): make invitation acceptance idempotent #25394

Open
opened 2026-04-15 22:52:00 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/9193
Author: @GautamBytes
Created: 4/14/2026
Status: 🔄 Open

Base: mainHead: fix/add-member-existence-check


📝 Commits (3)

  • c63f80d fix(org): dedupe invite acceptance
  • 1f2c2e7 fix(org): dedupe invite acceptance
  • 10ffd9c fix(kysely-adapter): only return insertId for INSERT/REPLACE queries

📊 Changes

6 files changed (+500 additions, -27 deletions)

View changed files

.changeset/fix-invite-accept-race.md (+5 -0)
📝 packages/better-auth/src/plugins/organization/adapter.ts (+32 -0)
📝 packages/better-auth/src/plugins/organization/organization.test.ts (+297 -7)
📝 packages/better-auth/src/plugins/organization/routes/crud-invites.ts (+132 -15)
📝 packages/kysely-adapter/src/node-sqlite-dialect.ts (+32 -3)
📝 packages/memory-adapter/src/memory-adapter.ts (+2 -2)

📄 Description

Summary

  • prevent duplicate organization members when acceptInvitation is called more than once for the same invitation
  • make repeated accepts reuse the existing member once the invitation is already accepted
  • guard the invitation state transition with a conditional update and restore team/session state on replay paths

Notes


Summary by cubic

Make organization invitation acceptance idempotent and race-safe to prevent duplicate members. Retries and concurrent accepts now reuse the existing member and restore active org/team in the session.

  • Bug Fixes
    • Guard invitation updates with expectedStatus: "pending" to prevent double-accepts; if already accepted, fetch the existing member (with brief retries), set active org, and restore team when applicable.
    • Check for an existing org member before creating one; create missing team memberships and enforce team limits.
    • Update kysely-adapter SQLite dialect to return affected row counts for non-returning mutations and only return insertId for INSERT/REPLACE queries.
    • Make memory-adapter updateMany async and return the number of updated records; expand tests for retries and concurrent accepts; add a patch changeset for better-auth.

Written for commit 10ffd9cdb5. Summary will update 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/9193 **Author:** [@GautamBytes](https://github.com/GautamBytes) **Created:** 4/14/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/add-member-existence-check` --- ### 📝 Commits (3) - [`c63f80d`](https://github.com/better-auth/better-auth/commit/c63f80d72df36839ddb88c18e8a492ee7de0cc39) fix(org): dedupe invite acceptance - [`1f2c2e7`](https://github.com/better-auth/better-auth/commit/1f2c2e7bb9c1fe4288be4905d6b0e5fe90492642) fix(org): dedupe invite acceptance - [`10ffd9c`](https://github.com/better-auth/better-auth/commit/10ffd9cdb5f3f7028751cfc7f3aa88660a8046e1) fix(kysely-adapter): only return insertId for INSERT/REPLACE queries ### 📊 Changes **6 files changed** (+500 additions, -27 deletions) <details> <summary>View changed files</summary> ➕ `.changeset/fix-invite-accept-race.md` (+5 -0) 📝 `packages/better-auth/src/plugins/organization/adapter.ts` (+32 -0) 📝 `packages/better-auth/src/plugins/organization/organization.test.ts` (+297 -7) 📝 `packages/better-auth/src/plugins/organization/routes/crud-invites.ts` (+132 -15) 📝 `packages/kysely-adapter/src/node-sqlite-dialect.ts` (+32 -3) 📝 `packages/memory-adapter/src/memory-adapter.ts` (+2 -2) </details> ### 📄 Description ## Summary - prevent duplicate organization members when `acceptInvitation` is called more than once for the same invitation - make repeated accepts reuse the existing member once the invitation is already accepted - guard the invitation state transition with a conditional update and restore team/session state on replay paths ## Notes - Closes #9154 - Adds a patch changeset for `better-auth` - No breaking changes <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Make organization invitation acceptance idempotent and race-safe to prevent duplicate members. Retries and concurrent accepts now reuse the existing member and restore active org/team in the session. - **Bug Fixes** - Guard invitation updates with `expectedStatus: "pending"` to prevent double-accepts; if already accepted, fetch the existing member (with brief retries), set active org, and restore team when applicable. - Check for an existing org member before creating one; create missing team memberships and enforce team limits. - Update `kysely-adapter` SQLite dialect to return affected row counts for non-returning mutations and only return `insertId` for INSERT/REPLACE queries. - Make `memory-adapter` `updateMany` async and return the number of updated records; expand tests for retries and concurrent accepts; add a patch changeset for `better-auth`. <sup>Written for commit 10ffd9cdb5f3f7028751cfc7f3aa88660a8046e1. 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:52:00 -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#25394