[GH-ISSUE #6800] API Key plugin: MongoDB adapter fails with '_id' immutable field error #19265

Closed
opened 2026-04-15 18:08:01 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @Timtech4u on GitHub (Dec 16, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/6800

Description

When using the API Key plugin with the MongoDB adapter, API key verification fails with:

MongoServerError: Performing an update on the path '_id' would modify the immutable field '_id'

Environment

  • better-auth: v1.4.7
  • Database: MongoDB (via official MongoDB adapter)
  • Plugin: @better-auth/plugins/api-key

Root Cause

In plugins/api-key/routes/verify-api-key.mjs (around line 106-119), the update payload spreads the entire apiKey object:

const updated = {
  ...apiKey,  // <-- Includes `id` field
  ...update,
  remaining,
  lastRefillAt,
  updatedAt: new Date()
};
await ctx.context.adapter.update({
  model: API_KEY_TABLE_NAME,
  update: updated  // `id` gets transformed to `_id` by MongoDB adapter
});

The MongoDB adapter transforms id_id for storage. When this transformed object is passed to $set, MongoDB rejects it because _id is immutable.

Expected Behavior

API key verification should complete successfully, updating only mutable fields.

Suggested Fix

Destructure to exclude id before spreading:

const { id, ...apiKeyData } = apiKey;
const updated = {
  ...apiKeyData,
  ...update,
  remaining,
  lastRefillAt,
  updatedAt: new Date()
};

Reproduction

  1. Configure Better Auth with MongoDB adapter
  2. Enable API Key plugin
  3. Create an API key
  4. Make a request with x-api-key header
  5. Observe the MongoDB error on the server
Originally created by @Timtech4u on GitHub (Dec 16, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/6800 ## Description When using the API Key plugin with the MongoDB adapter, API key verification fails with: ``` MongoServerError: Performing an update on the path '_id' would modify the immutable field '_id' ``` ## Environment - better-auth: v1.4.7 - Database: MongoDB (via official MongoDB adapter) - Plugin: `@better-auth/plugins/api-key` ## Root Cause In `plugins/api-key/routes/verify-api-key.mjs` (around line 106-119), the update payload spreads the entire `apiKey` object: ```javascript const updated = { ...apiKey, // <-- Includes `id` field ...update, remaining, lastRefillAt, updatedAt: new Date() }; await ctx.context.adapter.update({ model: API_KEY_TABLE_NAME, update: updated // `id` gets transformed to `_id` by MongoDB adapter }); ``` The MongoDB adapter transforms `id` → `_id` for storage. When this transformed object is passed to `$set`, MongoDB rejects it because `_id` is immutable. ## Expected Behavior API key verification should complete successfully, updating only mutable fields. ## Suggested Fix Destructure to exclude `id` before spreading: ```javascript const { id, ...apiKeyData } = apiKey; const updated = { ...apiKeyData, ...update, remaining, lastRefillAt, updatedAt: new Date() }; ``` ## Reproduction 1. Configure Better Auth with MongoDB adapter 2. Enable API Key plugin 3. Create an API key 4. Make a request with `x-api-key` header 5. Observe the MongoDB error on the server
GiteaMirror added the lockedbug labels 2026-04-15 18:08:01 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Dec 16, 2025):

Thanks for reporting this issue. It appears that the update payload includes the API key’s id, which gets transformed into the immutable _id field by the MongoDB adapter. The recommended approach is to first destructure the API key object to remove the id field, then spread the remaining properties when merging with the update payload. This should prevent MongoDB from trying to modify the _id field during the update. Could you try this change and let us know if it resolves the error? Tag @better-auth-agent if you would like an answer from the Agent.

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3660932565 --> @better-auth-agent[bot] commented on GitHub (Dec 16, 2025): Thanks for reporting this issue. It appears that the update payload includes the API key’s id, which gets transformed into the immutable _id field by the MongoDB adapter. The recommended approach is to first destructure the API key object to remove the id field, then spread the remaining properties when merging with the update payload. This should prevent MongoDB from trying to modify the _id field during the update. Could you try this change and let us know if it resolves the error? Tag @better-auth-agent if you would like an answer from the Agent. _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@Bekacru commented on GitHub (Dec 16, 2025):

https://github.com/better-auth/better-auth/pull/6752

this shoudl address the issue. feel free to reopen if not

<!-- gh-comment-id:3662579044 --> @Bekacru commented on GitHub (Dec 16, 2025): https://github.com/better-auth/better-auth/pull/6752 this shoudl address the issue. feel free to reopen if not
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#19265