[GH-ISSUE #3172] database mongodbAdapter options.advanced.generateId is deprecated and is the only option checked in the create method #9501

Closed
opened 2026-04-13 04:59:15 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @Ngorror on GitHub (Jun 25, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3172

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Set your betterAuth server with the following options:
export const auth = betterAuth({
  advanced: {
    database: {
      generateId: createId,
    },
  /* do not uncomment the following line since it is deprecated
   * @see https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/types/options.ts#L733
   */
  // generateId: createId   
  },
  database: mongodbAdapter(db),
  emailAndPassword: { enabled: true },
});
  1. Sign up a new user
  2. Check the user collection for the newly created user - there is no id field in it, as is expected to be generated by your function

Current vs. Expected behavior

the user to have my custom id

Although the check is only on the options.advanced.generatedId

const hasCustomId = options.advanced?.generateId;

see implementation

What version of Better Auth are you using?

1.2.9

Provide environment information

- OS: ubuntu (mint)
- node: 24.2.0
- browser: Chrome

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

Backend

Auth config (if applicable)

import { createId } from '@paralleldrive/cuid2';
import { betterAuth } from 'better-auth';
import { mongodbAdapter } from 'better-auth/adapters/mongodb';

const client = new MongoClient('mongodb://localhost:27017/my-db');

export const db = client.db();

export const auth = betterAuth({
  advanced: {
    database: {
      generateId: createId,
    },
  },
  database: mongodbAdapter(db),
  emailAndPassword: { enabled: true },
});

Additional context

Possible fix:
just check for both options.advanced.generateId and options.advanced.database.generateId like

	const customIdGen =
		options.advanced?.database?.generateId || options.advanced?.generateId;

see implementation

Originally created by @Ngorror on GitHub (Jun 25, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3172 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Set your betterAuth server with the following options: ```js export const auth = betterAuth({ advanced: { database: { generateId: createId, }, /* do not uncomment the following line since it is deprecated * @see https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/types/options.ts#L733 */ // generateId: createId }, database: mongodbAdapter(db), emailAndPassword: { enabled: true }, }); ``` 2. Sign up a new user 3. Check the `user` collection for the newly created user - there is no `id` field in it, as is expected to be generated by your function ### Current vs. Expected behavior the user to have my custom id Although the check is only on the `options.advanced.generatedId` ```js const hasCustomId = options.advanced?.generateId; ``` see [implementation](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/adapters/mongodb-adapter/mongodb-adapter.ts#L230) ### What version of Better Auth are you using? 1.2.9 ### Provide environment information ```bash - OS: ubuntu (mint) - node: 24.2.0 - browser: Chrome ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { createId } from '@paralleldrive/cuid2'; import { betterAuth } from 'better-auth'; import { mongodbAdapter } from 'better-auth/adapters/mongodb'; const client = new MongoClient('mongodb://localhost:27017/my-db'); export const db = client.db(); export const auth = betterAuth({ advanced: { database: { generateId: createId, }, }, database: mongodbAdapter(db), emailAndPassword: { enabled: true }, }); ``` ### Additional context Possible fix: just check for both `options.advanced.generateId` and `options.advanced.database.generateId` like ```js const customIdGen = options.advanced?.database?.generateId || options.advanced?.generateId; ``` see [implementation](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/adapters/mongodb-adapter/mongodb-adapter.ts#L11)
GiteaMirror added the locked label 2026-04-13 04:59:15 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jun 25, 2025):

Hi @Ngorror! I'm Dosu and I’m helping the better-auth team.

You're right—there's a misalignment in the MongoDB adapter: while the transform logic supports both options.advanced.database.generateId and the deprecated options.advanced.generateId, the create method only checks the deprecated property. This means your custom ID generator at options.advanced.database.generateId isn't used as expected in v1.2.9, which matches what you're seeing in practice. The types and docs indicate you should use the nested property, but the implementation isn't fully aligned yet (sourcetypes).

Your suggested fix—to check both properties—matches the intended direction and would resolve the issue. As a workaround for now, you can temporarily set your custom generator at the deprecated options.advanced.generateId property (with a comment noting the deprecation) until the adapter is updated to check both. There are recent PRs and ongoing work around custom ID generation, but nothing merged yet that addresses this specific bug (PR example).

If you want to avoid the deprecated property, you could also patch the adapter locally with your fix until an official update lands.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

<!-- gh-comment-id:3006083329 --> @dosubot[bot] commented on GitHub (Jun 25, 2025): <!-- Greeting --> Hi @Ngorror! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> You're right—there's a misalignment in the MongoDB adapter: while the transform logic supports both `options.advanced.database.generateId` and the deprecated `options.advanced.generateId`, the `create` method only checks the deprecated property. This means your custom ID generator at `options.advanced.database.generateId` isn't used as expected in v1.2.9, which matches what you're seeing in practice. The types and docs indicate you should use the nested property, but the implementation isn't fully aligned yet ([source](https://github.com/better-auth/better-auth/blob/76e186903e52607caf0460cc2da0066adb574b2c/packages/better-auth/src/adapters/mongodb-adapter/mongodb-adapter.ts), [types](https://github.com/better-auth/better-auth/blob/76e186903e52607caf0460cc2da0066adb574b2c/packages/better-auth/src/types/options.ts)). Your suggested fix—to check both properties—matches the intended direction and would resolve the issue. As a workaround for now, you can temporarily set your custom generator at the deprecated `options.advanced.generateId` property (with a comment noting the deprecation) until the adapter is updated to check both. There are recent PRs and ongoing work around custom ID generation, but nothing merged yet that addresses this specific bug ([PR example](https://github.com/better-auth/better-auth/pull/3048)). If you want to avoid the deprecated property, you could also patch the adapter locally with your fix until an official update lands. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/372f3cf3-750b-44a9-a506-e5e593f751db?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/372f3cf3-750b-44a9-a506-e5e593f751db?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/372f3cf3-750b-44a9-a506-e5e593f751db?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/372f3cf3-750b-44a9-a506-e5e593f751db?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/372f3cf3-750b-44a9-a506-e5e593f751db?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/372f3cf3-750b-44a9-a506-e5e593f751db?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/372f3cf3-750b-44a9-a506-e5e593f751db?feedback_type=other)</sup>&nbsp;&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/3172)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9501