How do I setup id when creating new users?

I am trying to use stripe plugin
databaseHooks: {
user: {
create: {
before: async (user, context) => {
// Generate user ID from email
console.log("User before creation:", user);


if (user.email) {
const customId = generateUserId(user.email);

return {
data: {
...user,
id: customId
},
forceAllowId: true // This is required to allow custom IDs
};
}

// If no email is available, let BetterAuth generate the ID
return { data: user };
},
after: async (user) => {
// Perform actions after user creation
console.log("User after creation:", user);
}
},
databaseHooks: {
user: {
create: {
before: async (user, context) => {
// Generate user ID from email
console.log("User before creation:", user);


if (user.email) {
const customId = generateUserId(user.email);

return {
data: {
...user,
id: customId
},
forceAllowId: true // This is required to allow custom IDs
};
}

// If no email is available, let BetterAuth generate the ID
return { data: user };
},
after: async (user) => {
// Perform actions after user creation
console.log("User after creation:", user);
}
},
2025-06-02T19:51:30.109Z WARN [Better Auth]: [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: at createWithHooks (webpack-internal:///(rsc)/./node_modules/better-auth/dist/shared/better-auth.XjdOGtZf.mjs:78:85) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Object.createOAuthUser (webpack-internal:///(rsc)/./node_modules/better-auth/dist/shared/better-auth.XjdOGtZf.mjs:158:27) at async handleOAuthUserInfo (webpack-internal:///(rsc)/./node_modules/better-auth/dist/shared/better-auth.Dvh-YFwT.mjs:1285:14)
14 Replies
ilya
ilya4mo ago
Have the same problem, did you manage to solve it?
Ping
Ping4mo ago
Right now there isn't a way to generate an ID like this. Your best bet if you really want a custom ID for users is to pass an id generator in your auth config via advanced.database.generateId
ilya
ilya4mo ago
@Ping I do need, cause I have to maintain backward compatibility with my DB. my user.id filed called user.userId, and I have to keep the id and userId equal during user creation. Unless you can suggest any other way to do so. For now, I had to downgrade the library to 1.2.3 version. Here is my code:
databaseHooks: {
user: {
create: {
before: async (user) => {
const userId = generateId(24);
return {
data: {
...user,
id: userId,
userId, // id alias
subscriptionStatus: SubscriptionStatus.FREE,
primaryProvider: IdentityProvider.email,
},
};
},
after: async (user) => {
await streamClient.upsertUsers([
{
id: user.id,
name: user.email,
first_name: user.email,
email: user.email,
},
]);

await cqrsEvent({
userId: user.id,
eventName: TopologyEvents.PROFILE_CREATED,
data: {},
});
},
},
},
},
databaseHooks: {
user: {
create: {
before: async (user) => {
const userId = generateId(24);
return {
data: {
...user,
id: userId,
userId, // id alias
subscriptionStatus: SubscriptionStatus.FREE,
primaryProvider: IdentityProvider.email,
},
};
},
after: async (user) => {
await streamClient.upsertUsers([
{
id: user.id,
name: user.email,
first_name: user.email,
email: user.email,
},
]);

await cqrsEvent({
userId: user.id,
eventName: TopologyEvents.PROFILE_CREATED,
data: {},
});
},
},
},
},
Can you please advice?
Ping
Ping4mo ago
Opened a PR to support passing id. You can track it here: https://github.com/better-auth/better-auth/pull/3048
GitHub
feat(database-hooks): Allow passing id in DB hook create by pin...
It's the same to using a custom idGenerator, except configurable by the database hook which would in theory provide more data. A use-case is to generate the id based on user info in the use...
ilya
ilya4mo ago
Thanks! I see it doesn't pass the tests
Cory
Cory4mo ago
is there a way to implement something similar for organizations? A littile different, but basically I want to create a column based off the id, but the ID is not passed to the "before" db hook for the organization
Cory
Cory4mo ago
No description
Ping
Ping4mo ago
Not right now, but once database hooks are supported across all tables, you should be able to achieve this using DB hooks.
Cory
Cory4mo ago
ok
ilya
ilya4mo ago
@Ping It seems like the user.id is not passed in the regular before create dbhook eaither. Here is my code:
databaseHooks: {
user: {
create: {
before: async (user) => {
console.log(user);
const userId = generateId(24);
return {
data: {
...user,
// id: userId,
userId, // id alias
databaseHooks: {
user: {
create: {
before: async (user) => {
console.log(user);
const userId = generateId(24);
return {
data: {
...user,
// id: userId,
userId, // id alias
And here is the log of user that I'm getting:
ANY /hono/api/auth/sign-in/email-otp (λ: honoApp)
{
createdAt: 2025-06-23T15:46:46.122Z,
updatedAt: 2025-06-23T15:46:46.122Z,
emailVerified: true,
email: 'ilya+registerbetter@agavehealth.com',
name: ''
}
ANY /hono/api/auth/sign-in/email-otp (λ: honoApp)
{
createdAt: 2025-06-23T15:46:46.122Z,
updatedAt: 2025-06-23T15:46:46.122Z,
emailVerified: true,
email: 'ilya+registerbetter@agavehealth.com',
name: ''
}
Is there any way to get user.id in the database before hook?
Ping
Ping4mo ago
When DB hooks are supported for all tables is when this will be supported, right now you can't
ilya
ilya4mo ago
So there is no way for me to set a duplicated id for any other field in the table rn? Do you have an ETA when it will be supported? @Ping
Ping
Ping4mo ago
Not sure, it will definitely be after 1.3. We need all adapters to use our createAdapter wrapper function in order for us to start work on features like this. Right now mongoDb adpater doesn't use it, and other community made ones are not either. It may take some time as we will need to add warnings to users for those who are using adapters which doesn't use createAdapter and stuff to alert the adapter maintainers to update...
ilya
ilya4mo ago
Got you, thank you for the response. I'm now trying to use the after: hook

Did you find this page helpful?