[PR #5095] [MERGED] fix(organization): prevent duplicate slug on organization update #14018

Closed
opened 2026-04-13 09:16:06 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5095
Author: @kira-1011
Created: 10/5/2025
Status: Merged
Merged: 10/15/2025
Merged by: @Bekacru

Base: canaryHead: fix/organization-update-slug-validation


📝 Commits (10+)

  • 3c74e87 fix(organization): prevent duplicate slug on organization update
  • 48b05a7 fix: code format
  • 0a81176 Merge branch 'canary' into fix/organization-update-slug-validation
  • f513262 chore: check typeof
  • 6417add Merge branch 'canary' into fix/organization-update-slug-validation
  • 211a1b4 fix(organization): use specific error for duplicate slug on update
  • f06815d Merge branch 'canary' into fix/organization-update-slug-validation
  • 536625a Merge branch 'canary' into fix/organization-update-slug-validation
  • a314713 Merge branch 'canary' into fix/organization-update-slug-validation
  • fd87926 Merge branch 'canary' into fix/organization-update-slug-validation

📊 Changes

3 files changed (+36 additions, -0 deletions)

View changed files

📝 packages/better-auth/src/plugins/organization/error-codes.ts (+1 -0)
📝 packages/better-auth/src/plugins/organization/organization.test.ts (+21 -0)
📝 packages/better-auth/src/plugins/organization/routes/crud-org.ts (+14 -0)

📄 Description

Problem

The updateOrganization endpoint was missing slug uniqueness validation. This allowed users to update an organization's slug to one that's already in use by another organization, causing a database constraint error (500) instead of a proper validation error (400).

Steps to Reproduce

  1. Create organization with slug a
  2. Create organization with slug b
  3. Update organization b to change its slug to a
  4. Both organizations now have slug a (database error)

Solution

Added slug uniqueness validation in the updateOrganization function before the update is performed

Changes

  • Added slug uniqueness check in organization update endpoint
  • Returns ORGANIZATION_SLUG_ALREADY_EXISTS error when duplicate detected
  • Allows organization to keep its own slug when updating other fields

Testing

  • Prevents duplicate slug assignment
  • Returns proper 400 error instead of 500

Closes #5093


Summary by cubic

Prevent duplicate organization slugs on update by validating uniqueness before saving. This avoids a 500 database error and returns a 400 with ORGANIZATION_ALREADY_EXISTS.

  • Bug Fixes
    • Added slug uniqueness check in updateOrganization; keeps existing slug when updating other fields.
    • Added test to ensure duplicate slug updates fail with 400 and ORGANIZATION_ALREADY_EXISTS.

🔄 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/5095 **Author:** [@kira-1011](https://github.com/kira-1011) **Created:** 10/5/2025 **Status:** ✅ Merged **Merged:** 10/15/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `fix/organization-update-slug-validation` --- ### 📝 Commits (10+) - [`3c74e87`](https://github.com/better-auth/better-auth/commit/3c74e8788a26607f94f506e8088919587009c92e) fix(organization): prevent duplicate slug on organization update - [`48b05a7`](https://github.com/better-auth/better-auth/commit/48b05a76c15adbce9c63a407713f2286e8ad820e) fix: code format - [`0a81176`](https://github.com/better-auth/better-auth/commit/0a81176e727f2a6e2a20af854d754a5dcfe1f796) Merge branch 'canary' into fix/organization-update-slug-validation - [`f513262`](https://github.com/better-auth/better-auth/commit/f51326288aab610a6f32a3fd74813ab1cd3115a7) chore: check typeof - [`6417add`](https://github.com/better-auth/better-auth/commit/6417add83f4e9dae4b0decd8037bc993d889377c) Merge branch 'canary' into fix/organization-update-slug-validation - [`211a1b4`](https://github.com/better-auth/better-auth/commit/211a1b4161dc8c4697f169711615c216e9cdcc82) fix(organization): use specific error for duplicate slug on update - [`f06815d`](https://github.com/better-auth/better-auth/commit/f06815dbf544b787d5a437dda5b1fed02047837d) Merge branch 'canary' into fix/organization-update-slug-validation - [`536625a`](https://github.com/better-auth/better-auth/commit/536625aba277024f22188a3f8bced18aa7263681) Merge branch 'canary' into fix/organization-update-slug-validation - [`a314713`](https://github.com/better-auth/better-auth/commit/a314713ec4139220b2866d505d728c94bf538dda) Merge branch 'canary' into fix/organization-update-slug-validation - [`fd87926`](https://github.com/better-auth/better-auth/commit/fd87926c2664b1e367ad5dd2f73dedb50b1bd8b3) Merge branch 'canary' into fix/organization-update-slug-validation ### 📊 Changes **3 files changed** (+36 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/organization/error-codes.ts` (+1 -0) 📝 `packages/better-auth/src/plugins/organization/organization.test.ts` (+21 -0) 📝 `packages/better-auth/src/plugins/organization/routes/crud-org.ts` (+14 -0) </details> ### 📄 Description ## Problem The `updateOrganization` endpoint was missing slug uniqueness validation. This allowed users to update an organization's slug to one that's already in use by another organization, causing a database constraint error (500) instead of a proper validation error (400). ## Steps to Reproduce 1. Create organization with slug `a` 2. Create organization with slug `b` 3. Update organization `b` to change its slug to `a` 4. Both organizations now have slug `a` (database error) ## Solution Added slug uniqueness validation in the `updateOrganization` function before the update is performed ## Changes - Added slug uniqueness check in organization update endpoint - Returns `ORGANIZATION_SLUG_ALREADY_EXISTS` error when duplicate detected - Allows organization to keep its own slug when updating other fields ## Testing - [x] Prevents duplicate slug assignment - [x] Returns proper 400 error instead of 500 Closes #5093 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Prevent duplicate organization slugs on update by validating uniqueness before saving. This avoids a 500 database error and returns a 400 with ORGANIZATION_ALREADY_EXISTS. - **Bug Fixes** - Added slug uniqueness check in updateOrganization; keeps existing slug when updating other fields. - Added test to ensure duplicate slug updates fail with 400 and ORGANIZATION_ALREADY_EXISTS. <!-- 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-13 09:16: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#14018