[GH-ISSUE #2186] MongodbAdapter: additionalFields, unique field is not supported? #9092

Closed
opened 2026-04-13 04:23:59 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @Nicolab on GitHub (Apr 8, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2186

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

I set unique: true with mongodbAdapter

export const auth = betterAuth({
  database: mongodbAdapter(db),
 user: {
  additionalFields: {
      displayName: {
        type: "string",
        label: "Display name",
        description: "Display name of the user",
        minLength: 2,
        maxLength: 25,
        required: false,
        nullable: true,
        unique: true,
      },
}
})

Register an user with same displayName (unique true) is possible.

Current vs. Expected behavior

It shouldn't be possible to register 2 users with same displayName (unique: true).

What version of Better Auth are you using?

1.2.5

Provide environment information

Bun.js in Docker (Ubuntu 24.04 LTS)

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
import { mongodbAdapter } from "better-auth/adapters/mongodb";
import { db } from "../db/mongo";

export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
 database: mongodbAdapter(db),
 user: {
  additionalFields: {
      displayName: {
        type: "string",
        label: "Display name",
        description: "Display name of the user",
        minLength: 2,
        maxLength: 25,
        required: false,
        nullable: true,
        unique: true,
      },
}
});

Additional context

Registered with auth.api.signUpEmail()

Originally created by @Nicolab on GitHub (Apr 8, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2186 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce I set `unique: true` with mongodbAdapter ```ts export const auth = betterAuth({ database: mongodbAdapter(db), user: { additionalFields: { displayName: { type: "string", label: "Display name", description: "Display name of the user", minLength: 2, maxLength: 25, required: false, nullable: true, unique: true, }, } }) ``` Register an user with same displayName (unique true) is possible. ### Current vs. Expected behavior It shouldn't be possible to register 2 users with same displayName (unique: true). ### What version of Better Auth are you using? 1.2.5 ### Provide environment information ```bash Bun.js in Docker (Ubuntu 24.04 LTS) ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" import { mongodbAdapter } from "better-auth/adapters/mongodb"; import { db } from "../db/mongo"; export const auth = betterAuth({ emailAndPassword: { enabled: true }, database: mongodbAdapter(db), user: { additionalFields: { displayName: { type: "string", label: "Display name", description: "Display name of the user", minLength: 2, maxLength: 25, required: false, nullable: true, unique: true, }, } }); ``` ### Additional context Registered with `auth.api.signUpEmail()`
GiteaMirror added the locked label 2026-04-13 04:23:59 -05:00
Author
Owner

@Kinfe123 commented on GitHub (Apr 9, 2025):

till i look at the issue , this is the patten most of them uses -

export const auth = betterAuth({
  database: mongodbAdapter(db),
  user: {
    additionalFields: {
      displayName: {
        type: "string",
        // ... other config
      },
    },
    // Add validation hook
    beforeCreate: async (user) => {
      const existingUser = await db.collection('users').findOne({ 
        displayName: user.displayName 
      });
      if (existingUser) {
        throw new Error('Display name already taken');
      }
      return user;
    }
  }
});

you can use this to validate before hand and throw error

<!-- gh-comment-id:2790274269 --> @Kinfe123 commented on GitHub (Apr 9, 2025): till i look at the issue , this is the patten most of them uses - ```ts export const auth = betterAuth({ database: mongodbAdapter(db), user: { additionalFields: { displayName: { type: "string", // ... other config }, }, // Add validation hook beforeCreate: async (user) => { const existingUser = await db.collection('users').findOne({ displayName: user.displayName }); if (existingUser) { throw new Error('Display name already taken'); } return user; } } }); ``` you can use this to validate before hand and throw error
Author
Owner

@Nicolab commented on GitHub (Apr 10, 2025):

Great, thank you very much for your help 👍

<!-- gh-comment-id:2795452238 --> @Nicolab commented on GitHub (Apr 10, 2025): Great, thank you very much for your help 👍
Author
Owner

@Nicolab commented on GitHub (Apr 11, 2025):

beforeCreate doesn't seem to be part of better-auth. This function is not executed and is no longer in the doc.

I've been looking for an alternative, I've tested database hooks and it works:
https://www.better-auth.com/docs/concepts/database#database-hooks
Hooks: user create and update
84194242ae/packages/better-auth/src/types/options.ts (L662)

<!-- gh-comment-id:2795633374 --> @Nicolab commented on GitHub (Apr 11, 2025): beforeCreate doesn't seem to be part of better-auth. This function is not executed and is no longer in the doc. I've been looking for an alternative, I've tested _database hooks_ and it works: https://www.better-auth.com/docs/concepts/database#database-hooks Hooks: user create and update https://github.com/better-auth/better-auth/blob/84194242ae495b48d705a023f489ccb7fbfcbd81/packages/better-auth/src/types/options.ts#L662
Author
Owner

@Kinfe123 commented on GitHub (Apr 11, 2025):

yeah yeah i mean to the hook beforeCreate is a part of org plugin . i mean to mention database hooks which both are kinda of the same...sorry for the confusion

<!-- gh-comment-id:2795742719 --> @Kinfe123 commented on GitHub (Apr 11, 2025): yeah yeah i mean to the hook beforeCreate is a part of org plugin . i mean to mention database hooks which both are kinda of the same...sorry for the confusion
Author
Owner

@Nicolab commented on GitHub (Apr 14, 2025):

@Kinfe123 No problem, I've added the additional information mainly for people who land on the issue after searching for this bug.

I see you closed this issue, does that mean it's fixed now (unique: true works)?

<!-- gh-comment-id:2800632426 --> @Nicolab commented on GitHub (Apr 14, 2025): @Kinfe123 No problem, I've added the additional information mainly for people who land on the issue after searching for this bug. I see you closed this issue, does that mean it's fixed now (unique: true works)?
Author
Owner

@Kinfe123 commented on GitHub (Apr 14, 2025):

There is already issue for that

<!-- gh-comment-id:2800634819 --> @Kinfe123 commented on GitHub (Apr 14, 2025): There is already issue for that
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9092