Generate Custom User id while creating a user #1297

Closed
opened 2026-03-13 08:31:51 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @meera on GitHub (Jun 2, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Upgraded to better-auth and this is breaking! How do I assign proper user-id to newly created users?

databaseHooks: {
  user: {
    create: {
        before: async (user) => {
            // Generate user ID from email
            const generateUserId = (email) => `look-up-old-db-and-match-user-id`;
            
            return {
               data: {
                  ...user,
                  id: generateUserId(user.email)
               }
               forceAllowId: true,
            }
        },

[Kysely Adapter] - You are trying to create a record with an id. This is not allowed as we handle id generation for you, unless you pass in the forceAllowId parameter. The id will be ignored.
Create method with id being called at:

Current vs. Expected behavior

Expected user id to 'look-up-old-db-and-match-user-id' , but now it is a unique string.

What version of Better Auth are you using?

v1.2.8

Provide environment information

- OS Mac
-

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

This was working in prior version

Originally created by @meera on GitHub (Jun 2, 2025). ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce Upgraded to better-auth and this is breaking! How do I assign proper user-id to newly created users? ``` databaseHooks: { user: { create: { before: async (user) => { // Generate user ID from email const generateUserId = (email) => `look-up-old-db-and-match-user-id`; return { data: { ...user, id: generateUserId(user.email) } forceAllowId: true, } }, ``` [Kysely Adapter] - You are trying to create a record with an id. This is not allowed as we handle id generation for you, unless you pass in the `forceAllowId` parameter. The id will be ignored. Create method with `id` being called at: ### Current vs. Expected behavior Expected user id to 'look-up-old-db-and-match-user-id' , but now it is a unique string. ### What version of Better Auth are you using? v1.2.8 ### Provide environment information ```bash - OS Mac - ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context This was working in prior version
Author
Owner

@rinshadkv commented on GitHub (Jun 11, 2025):

I may not fully understand your issue, but based on what I’ve gathered, you might want to try the following solution:

If your goal is to use database-specific ID generation (e.g., SERIAL in PostgreSQL), consider the following approach:

advanced: {
    database: {
      generateId: false,
    }
},
@rinshadkv commented on GitHub (Jun 11, 2025): I may not fully understand your issue, but based on what I’ve gathered, you might want to try the following solution: If your goal is to use database-specific ID generation (e.g., SERIAL in PostgreSQL), consider the following approach: ``` advanced: { advanced: { database: { generateId: false, } }, ```
Author
Owner

@ping-maxwell commented on GitHub (Jul 8, 2025):

Hey @meera can you confirm you're still facing this issue on the latest version of Better-auth?

@ping-maxwell commented on GitHub (Jul 8, 2025): Hey @meera can you confirm you're still facing this issue on the latest version of Better-auth?
Author
Owner

@ping-maxwell commented on GitHub (Jul 10, 2025):

I'm pretty sure we merged a PR a little ago which fixes this. I'll close this issue for now, if this still persists please let me know and I'll happily re-open this issue!

@ping-maxwell commented on GitHub (Jul 10, 2025): I'm pretty sure we merged a PR a little ago which fixes this. I'll close this issue for now, if this still persists please let me know and I'll happily re-open this issue!
Author
Owner

@zdiegoramos commented on GitHub (Jul 22, 2025):

Why does better-auth generate a random string for the ids? Is there a security reason for this? Are bigint autoincrement ids discouraged? Does better-auth expose ids to the client in any way?

@zdiegoramos commented on GitHub (Jul 22, 2025): Why does better-auth generate a random string for the ids? Is there a security reason for this? Are bigint autoincrement ids discouraged? Does better-auth expose ids to the client in any way?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1297