[GH-ISSUE #5377] Why was custom MongoDB ID generation removed in PR #4844? #10224

Closed
opened 2026-04-13 06:12:36 -05:00 by GiteaMirror · 8 comments
Owner

Originally created by @laakal on GitHub (Oct 17, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/5377

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Hi 👋,

I noticed that custom MongoDB ID generation support was removed in [PR #4844](https://github.com/better-auth/better-auth/pull/4844).

  1. Why was this functionality removed? @ping-maxwell
  2. Is there an alternative or recommended way to handle custom ID generation now?
  3. Would you consider reintroducing it (perhaps behind an option)?
export const auth = betterAuth({
  advanced: {
    database: {
      generateId: () => new ObjectId().toString(),
    },
  },
  database: mongodbAdapter(db),
  emailAndPassword: { enabled: true },
});

Current vs. Expected behavior

🧩 Current Behavior

After PR [#4844](https://github.com/better-auth/better-auth/pull/4844), custom MongoDB ID generation is no longer supported. The _id is now always generated automatically by MongoDB.

Expected Behavior

Developers should be able to provide a custom _id value when creating records, allowing integration with external systems or custom ID formats.

What version of Better Auth are you using?

1.3.27

System info

{
  "system": {
    "platform": "darwin",
    "arch": "arm64",
    "version": "Darwin Kernel Version 25.0.0: Wed Sep 17 21:41:50 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T6030",
    "release": "25.0.0",
    "cpuCount": 11,
    "cpuModel": "Apple M3 Pro",
    "totalMemory": "18.00 GB",
    "freeMemory": "0.13 GB"
  },
  "node": {
    "version": "v22.17.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "10.9.2"
  },
  "frameworks": [
    {
      "name": "express",
      "version": "^5.1.0"
    }
  ],
  "databases": [
    {
      "name": "mongodb",
      "version": "^6.20.0"
    }
  ],
  "betterAuth": {
    "version": "^1.3.27",
    "config": {
      "advanced": {
        "database": {}
      },
      "plugins": [
        {
          "name": "admin",
          "config": {
            "id": "admin",
            "hooks": {
              "after": [
                {}
              ]
            },
            "endpoints": {},
            "$ERROR_CODES": {
              "FAILED_TO_CREATE_USER": "Failed to create user",
              "USER_ALREADY_EXISTS": "User already exists.",
              "USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL": "User already exists. Use another email.",
              "YOU_CANNOT_BAN_YOURSELF": "You cannot ban yourself",
              "YOU_ARE_NOT_ALLOWED_TO_CHANGE_USERS_ROLE": "You are not allowed to change users role",
              "YOU_ARE_NOT_ALLOWED_TO_CREATE_USERS": "You are not allowed to create users",
              "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS": "You are not allowed to list users",
              "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS_SESSIONS": "You are not allowed to list users sessions",
              "YOU_ARE_NOT_ALLOWED_TO_BAN_USERS": "You are not allowed to ban users",
              "YOU_ARE_NOT_ALLOWED_TO_IMPERSONATE_USERS": "You are not allowed to impersonate users",
              "YOU_ARE_NOT_ALLOWED_TO_REVOKE_USERS_SESSIONS": "You are not allowed to revoke users sessions",
              "YOU_ARE_NOT_ALLOWED_TO_DELETE_USERS": "You are not allowed to delete users",
              "YOU_ARE_NOT_ALLOWED_TO_SET_USERS_PASSWORD": "[REDACTED]",
              "BANNED_USER": "You have been banned from this application",
              "YOU_ARE_NOT_ALLOWED_TO_GET_USER": "You are not allowed to get user",
              "NO_DATA_TO_UPDATE": "No data to update",
              "YOU_ARE_NOT_ALLOWED_TO_UPDATE_USERS": "You are not allowed to update users",
              "YOU_CANNOT_REMOVE_YOURSELF": "You cannot remove yourself"
            },
            "schema": {
              "user": {
                "fields": {
                  "role": {
                    "type": "string",
                    "required": false,
                    "input": false
                  },
                  "banned": {
                    "type": "boolean",
                    "defaultValue": false,
                    "required": false,
                    "input": false
                  },
                  "banReason": {
                    "type": "string",
                    "required": false,
                    "input": false
                  },
                  "banExpires": {
                    "type": "date",
                    "required": false,
                    "input": false
                  }
                }
              },
              "session": {
                "fields": {
                  "impersonatedBy": {
                    "type": "string",
                    "required": false
                  }
                }
              }
            },
            "options": {
              "defaultRole": "user"
            }
          }
        },
        {
          "name": "expo",
          "config": {
            "id": "expo",
            "hooks": {
              "after": [
                {}
              ]
            },
            "endpoints": {}
          }
        }
      ],
      "emailAndPassword": {
        "enabled": true
      },
      "session": {
        "freshAge": 10,
        "modelName": "sessions"
      },
      "user": {
        "modelName": "users",
        "additionalFields": {
          "role": {
            "type": "string",
            "defaultValue": "user"
          },
          "surname": {
            "type": "string"
          }
        }
      },
      "account": {
        "modelName": "accounts"
      },
      "verification": {
        "modelName": "verifications"
      },
      "socialProviders": {
        "github": {
          "clientId": "[REDACTED]",
          "clientSecret": "[REDACTED]"
        },
        "google": {
          "clientId": "[REDACTED]",
          "clientSecret": "[REDACTED]"
        }
      },
      "trustedOrigins": [
        "http://localhost:3044",
        "http://localhost:3000"
      ]
    }
  }
}

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

export const auth = betterAuth({
  advanced: {
    database: {
      generateId: () => new ObjectId().toString(),
    },
  },
  database: mongodbAdapter(db, {
    client,
  }),

  plugins: [admin({ defaultRole: 'user' }), expo()],
  emailAndPassword: {
    enabled: true,
  },
  session: {
    freshAge: 10,
    modelName: 'sessions',
  },
  user: {
    modelName: 'users',
    additionalFields: {
      role: {
        type: 'string',
        defaultValue: 'user',
      },
      surname: {
        type: 'string',
      },
    },
  },
  account: {
    modelName: 'accounts',
  },
  verification: {
    modelName: 'verifications',
  },

  socialProviders: {
    github: {
      clientId: process.env.GITHUB_CLIENT_ID as string,
      clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
    },
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,

      mapProfileToUser: (profile) => {
        const nameParts = profile.name?.split(' ') || [];
        const surname = nameParts[nameParts.length - 1] || '';
        const name =
          nameParts.length > 2
            ? nameParts.slice(0, -1).join(' ')
            : nameParts[0] || '';

        return { ...profile, surname, name };
      },
    },
  },
  trustedOrigins,
});

Additional context

No response

Originally created by @laakal on GitHub (Oct 17, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/5377 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Hi 👋, I noticed that **custom MongoDB ID generation support** was removed in [[PR #4844](https://github.com/better-auth/better-auth/pull/4844)](https://github.com/better-auth/better-auth/pull/4844). 1. Why was this functionality removed? @ping-maxwell 2. Is there an alternative or recommended way to handle custom ID generation now? 3. Would you consider reintroducing it (perhaps behind an option)? ```ts export const auth = betterAuth({ advanced: { database: { generateId: () => new ObjectId().toString(), }, }, database: mongodbAdapter(db), emailAndPassword: { enabled: true }, }); ``` ### Current vs. Expected behavior ### 🧩 Current Behavior After PR [[#4844](https://github.com/better-auth/better-auth/pull/4844)](https://github.com/better-auth/better-auth/pull/4844), custom MongoDB ID generation is no longer supported. The `_id` is now always generated automatically by MongoDB. ### ✅ Expected Behavior Developers should be able to provide a custom `_id` value when creating records, allowing integration with external systems or custom ID formats. ### What version of Better Auth are you using? 1.3.27 ### System info ```bash { "system": { "platform": "darwin", "arch": "arm64", "version": "Darwin Kernel Version 25.0.0: Wed Sep 17 21:41:50 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T6030", "release": "25.0.0", "cpuCount": 11, "cpuModel": "Apple M3 Pro", "totalMemory": "18.00 GB", "freeMemory": "0.13 GB" }, "node": { "version": "v22.17.0", "env": "development" }, "packageManager": { "name": "npm", "version": "10.9.2" }, "frameworks": [ { "name": "express", "version": "^5.1.0" } ], "databases": [ { "name": "mongodb", "version": "^6.20.0" } ], "betterAuth": { "version": "^1.3.27", "config": { "advanced": { "database": {} }, "plugins": [ { "name": "admin", "config": { "id": "admin", "hooks": { "after": [ {} ] }, "endpoints": {}, "$ERROR_CODES": { "FAILED_TO_CREATE_USER": "Failed to create user", "USER_ALREADY_EXISTS": "User already exists.", "USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL": "User already exists. Use another email.", "YOU_CANNOT_BAN_YOURSELF": "You cannot ban yourself", "YOU_ARE_NOT_ALLOWED_TO_CHANGE_USERS_ROLE": "You are not allowed to change users role", "YOU_ARE_NOT_ALLOWED_TO_CREATE_USERS": "You are not allowed to create users", "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS": "You are not allowed to list users", "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS_SESSIONS": "You are not allowed to list users sessions", "YOU_ARE_NOT_ALLOWED_TO_BAN_USERS": "You are not allowed to ban users", "YOU_ARE_NOT_ALLOWED_TO_IMPERSONATE_USERS": "You are not allowed to impersonate users", "YOU_ARE_NOT_ALLOWED_TO_REVOKE_USERS_SESSIONS": "You are not allowed to revoke users sessions", "YOU_ARE_NOT_ALLOWED_TO_DELETE_USERS": "You are not allowed to delete users", "YOU_ARE_NOT_ALLOWED_TO_SET_USERS_PASSWORD": "[REDACTED]", "BANNED_USER": "You have been banned from this application", "YOU_ARE_NOT_ALLOWED_TO_GET_USER": "You are not allowed to get user", "NO_DATA_TO_UPDATE": "No data to update", "YOU_ARE_NOT_ALLOWED_TO_UPDATE_USERS": "You are not allowed to update users", "YOU_CANNOT_REMOVE_YOURSELF": "You cannot remove yourself" }, "schema": { "user": { "fields": { "role": { "type": "string", "required": false, "input": false }, "banned": { "type": "boolean", "defaultValue": false, "required": false, "input": false }, "banReason": { "type": "string", "required": false, "input": false }, "banExpires": { "type": "date", "required": false, "input": false } } }, "session": { "fields": { "impersonatedBy": { "type": "string", "required": false } } } }, "options": { "defaultRole": "user" } } }, { "name": "expo", "config": { "id": "expo", "hooks": { "after": [ {} ] }, "endpoints": {} } } ], "emailAndPassword": { "enabled": true }, "session": { "freshAge": 10, "modelName": "sessions" }, "user": { "modelName": "users", "additionalFields": { "role": { "type": "string", "defaultValue": "user" }, "surname": { "type": "string" } } }, "account": { "modelName": "accounts" }, "verification": { "modelName": "verifications" }, "socialProviders": { "github": { "clientId": "[REDACTED]", "clientSecret": "[REDACTED]" }, "google": { "clientId": "[REDACTED]", "clientSecret": "[REDACTED]" } }, "trustedOrigins": [ "http://localhost:3044", "http://localhost:3000" ] } } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript export const auth = betterAuth({ advanced: { database: { generateId: () => new ObjectId().toString(), }, }, database: mongodbAdapter(db, { client, }), plugins: [admin({ defaultRole: 'user' }), expo()], emailAndPassword: { enabled: true, }, session: { freshAge: 10, modelName: 'sessions', }, user: { modelName: 'users', additionalFields: { role: { type: 'string', defaultValue: 'user', }, surname: { type: 'string', }, }, }, account: { modelName: 'accounts', }, verification: { modelName: 'verifications', }, socialProviders: { github: { clientId: process.env.GITHUB_CLIENT_ID as string, clientSecret: process.env.GITHUB_CLIENT_SECRET as string, }, google: { clientId: process.env.GOOGLE_CLIENT_ID as string, clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, mapProfileToUser: (profile) => { const nameParts = profile.name?.split(' ') || []; const surname = nameParts[nameParts.length - 1] || ''; const name = nameParts.length > 2 ? nameParts.slice(0, -1).join(' ') : nameParts[0] || ''; return { ...profile, surname, name }; }, }, }, trustedOrigins, }); ``` ### Additional context _No response_
GiteaMirror added the locked label 2026-04-13 06:12:36 -05:00
Author
Owner

@ping-maxwell commented on GitHub (Oct 17, 2025):

It shouldn't had been removed, I'm pretty sure we have unit tests for this 🤔
let me check

<!-- gh-comment-id:3414593318 --> @ping-maxwell commented on GitHub (Oct 17, 2025): It shouldn't had been removed, I'm pretty sure we have unit tests for this 🤔 let me check
Author
Owner

@ping-maxwell commented on GitHub (Oct 17, 2025):

@laakal are you saying this isn't working?
Because the functionality wasn't removed as far as I know

<!-- gh-comment-id:3414605798 --> @ping-maxwell commented on GitHub (Oct 17, 2025): @laakal are you saying this isn't working? Because the functionality wasn't removed as far as I know
Author
Owner

@ping-maxwell commented on GitHub (Oct 17, 2025):

Unit tests on this are passing 🤔
Image

<!-- gh-comment-id:3414615025 --> @ping-maxwell commented on GitHub (Oct 17, 2025): Unit tests on this are passing 🤔 <img width="642" height="587" alt="Image" src="https://github.com/user-attachments/assets/7c24451a-6a43-4fb2-b06a-1a4c035d9f22" />
Author
Owner

@laakal commented on GitHub (Oct 17, 2025):

@ping-maxwell Thanks for the reply. However, it’s still not working.
I’ve attached a sample video showing the issue.
It works correctly when using a version before the PR above, but it doesn’t work in the newer versions.

https://github.com/user-attachments/assets/d995a1e7-0de9-49d2-8c56-6ca64876a8d4

<!-- gh-comment-id:3414678320 --> @laakal commented on GitHub (Oct 17, 2025): @ping-maxwell Thanks for the reply. However, it’s still not working. I’ve attached a sample video showing the issue. It works correctly when using a version before the PR above, but it doesn’t work in the newer versions. https://github.com/user-attachments/assets/d995a1e7-0de9-49d2-8c56-6ca64876a8d4
Author
Owner

@ping-maxwell commented on GitHub (Oct 17, 2025):

Thanks for sharing the video, I'll test this locally and see what I can find.

<!-- gh-comment-id:3415037537 --> @ping-maxwell commented on GitHub (Oct 17, 2025): Thanks for sharing the video, I'll test this locally and see what I can find.
Author
Owner

@ping-maxwell commented on GitHub (Oct 17, 2025):

It seems to work locally for me.
I console.log the id here:

Image

This prints:

Image

and in MongoDB I see it works:

Image

If you console.logged yours, could you check if you have the same result as the one you see in your db?
I'm on 1.3.27

<!-- gh-comment-id:3415075455 --> @ping-maxwell commented on GitHub (Oct 17, 2025): It seems to work locally for me. I console.log the `id` here: <img width="423" height="163" alt="Image" src="https://github.com/user-attachments/assets/32baaea2-63cf-4cb3-9e27-77c0bf8ac64f" /> This prints: <img width="343" height="28" alt="Image" src="https://github.com/user-attachments/assets/a76c39f4-f2a2-4baa-abb6-09a41d6da308" /> and in MongoDB I see it works: <img width="702" height="185" alt="Image" src="https://github.com/user-attachments/assets/9b790e6c-b9ee-4148-90a5-3e63c79560ea" /> If you console.logged yours, could you check if you have the same result as the one you see in your db? I'm on 1.3.27
Author
Owner

@laakal commented on GitHub (Oct 17, 2025):

Okay, I think I understand your point now.
It seems we’re having a communication issue here.

There’s no problem calling the generateId function itself.
In our project, we want to store the _id value as a plain string, not as an ObjectId.
However, the return value of generateId is automatically converted to an ObjectId before being saved.

<!-- gh-comment-id:3415121585 --> @laakal commented on GitHub (Oct 17, 2025): Okay, I think I understand your point now. It seems we’re having a **communication issue** here. There’s **no problem calling the `generateId` function** itself. In our project, we want to **store the `_id` value as a plain string**, not as an `ObjectId`. However, the **return value of `generateId` is automatically converted to an `ObjectId`** before being saved.
Author
Owner

@ping-maxwell commented on GitHub (Oct 17, 2025):

Gotcha.
I'll open a PR to add support for changing the default field type of the id fields in mongo

<!-- gh-comment-id:3415141460 --> @ping-maxwell commented on GitHub (Oct 17, 2025): Gotcha. I'll open a PR to add support for changing the default field type of the id fields in mongo
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#10224