[PR #6752] [MERGED] fix(api-key): prevent id update error with MongoDB adapter #15113

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6752
Author: @balbuzar
Created: 12/14/2025
Status: Merged
Merged: 12/14/2025
Merged by: @Bekacru

Base: canaryHead: fix/api-key-mongodb-immutable-id


📝 Commits (1)

  • a05ac3a fix(apikey): prevent _id update error with MongoDB adapter

📊 Changes

1 file changed (+2 additions, -2 deletions)

View changed files

📝 packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts (+2 -2)

📄 Description

Description

Fixes MongoDB adapter compatibility with the API Key plugin by preventing attempts to update the immutable _id field.

Closes #6711

Problem

The API Key plugin tests are currently failing when run with MongoDB adapter (testWith: "mongodb").

Test Results:

  • Before fix: Tests 10 failed | 106 passed (116) with MongoDB
  • After fix: Tests 116 passed (116) with MongoDB

MongoDB throws an error when attempting to update the _id field:

codeName: "ImmutableField"
[...]
Plan executor error during findAndModify :: caused by :: Performing an update on the path '_id' would modify the immutable field '_id'

The _id field is immutable in MongoDB, but the update operation was including it in the update payload.

Solution

Explicitly exclude the id field from the update object by setting it to undefined before passing it to the adapter update method.

Changes

  • Modified the update call in API Key plugin to exclude id from the update payload: update: { ...updated, id: undefined }
  • Ensures MongoDB adapter compatibility without affecting other adapters

Testing

  • All API Key plugin tests now pass with MongoDB adapter (116/116)
  • No regression with other adapters expected (id: undefined is safely ignored)

Summary by cubic

Prevents MongoDB _id update errors in the API Key plugin by excluding the id field from update payloads. Restores MongoDB adapter compatibility; all API Key tests pass.

  • Bug Fixes
    • Update calls now set id: undefined to avoid modifying the immutable _id on MongoDB.

Written for commit a05ac3a363. Summary will update automatically 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/6752 **Author:** [@balbuzar](https://github.com/balbuzar) **Created:** 12/14/2025 **Status:** ✅ Merged **Merged:** 12/14/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `fix/api-key-mongodb-immutable-id` --- ### 📝 Commits (1) - [`a05ac3a`](https://github.com/better-auth/better-auth/commit/a05ac3a363ef7c569796c2f9a70bab54cc4acbce) fix(apikey): prevent _id update error with MongoDB adapter ### 📊 Changes **1 file changed** (+2 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/api-key/routes/verify-api-key.ts` (+2 -2) </details> ### 📄 Description ## Description Fixes MongoDB adapter compatibility with the API Key plugin by preventing attempts to update the immutable `_id` field. Closes #6711 ## Problem The API Key plugin tests are currently failing when run with MongoDB adapter (`testWith: "mongodb"`). **Test Results:** - ❌ **Before fix:** `Tests 10 failed | 106 passed (116)` with MongoDB - ✅ **After fix:** `Tests 116 passed (116)` with MongoDB MongoDB throws an error when attempting to update the `_id` field: ``` codeName: "ImmutableField" [...] Plan executor error during findAndModify :: caused by :: Performing an update on the path '_id' would modify the immutable field '_id' ``` The `_id` field is immutable in MongoDB, but the update operation was including it in the update payload. ## Solution Explicitly exclude the `id` field from the update object by setting it to `undefined` before passing it to the adapter update method. ## Changes - Modified the `update` call in API Key plugin to exclude `id` from the update payload: `update: { ...updated, id: undefined }` - Ensures MongoDB adapter compatibility without affecting other adapters ## Testing - ✅ All API Key plugin tests now pass with MongoDB adapter (116/116) - ✅ No regression with other adapters expected (id: undefined is safely ignored) <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Prevents MongoDB _id update errors in the API Key plugin by excluding the id field from update payloads. Restores MongoDB adapter compatibility; all API Key tests pass. - **Bug Fixes** - Update calls now set id: undefined to avoid modifying the immutable _id on MongoDB. <sup>Written for commit a05ac3a363ef7c569796c2f9a70bab54cc4acbce. Summary will update automatically 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-13 09:50:13 -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#15113