How to generate a custom userID on registration

Can I provide a custom ID when a user signs up? (or ideally call a custom endpoint so that my database generates it naturally) context: my database uses a custom primary key type
13 Replies
nikatune
nikatune2mo ago
create database hook user : { before : { } }
Geospace
GeospaceOP2mo ago
you 100% certain this works? I tried this a couple of months ago using async and it didn't work
nikatune
nikatune2mo ago
it will work
nikatune
nikatune2mo ago
import { betterAuth } from "better-auth";

export const auth = betterAuth({
databaseHooks: {
user: {
create: {
before: async (user, ctx) => {
// Modify the user object before it is created
return {
data: {
...user,
firstName: user.name.split(" ")[0],
lastName: user.name.split(" ")[1],
},
};
},
after: async (user) => {
//perform additional actions, like creating a stripe customer
},
},
},
},
});
import { betterAuth } from "better-auth";

export const auth = betterAuth({
databaseHooks: {
user: {
create: {
before: async (user, ctx) => {
// Modify the user object before it is created
return {
data: {
...user,
firstName: user.name.split(" ")[0],
lastName: user.name.split(" ")[1],
},
};
},
after: async (user) => {
//perform additional actions, like creating a stripe customer
},
},
},
},
});
Geospace
GeospaceOP2mo ago
export default {
user: {
create: {
before: async (user, ctx) => {
// const id = await fetchGeneratedID();

return {
data: {
user: {
...user,
id: "123",
},
},
};
},
},
},
};
export default {
user: {
create: {
before: async (user, ctx) => {
// const id = await fetchGeneratedID();

return {
data: {
user: {
...user,
id: "123",
},
},
};
},
},
},
};
id = uXWITtFBL9DqdebtBfUn3tgZ4JIhjVdv in my database
Ping
Ping2mo ago
@Geospace What version are you on? it should work on latest
Geospace
GeospaceOP2mo ago
ah i'm on 1.2.7, i assumed because that github issue was still open it hadn't been completed yet
Ping
Ping2mo ago
Can you link me that issue?
Ping
Ping2mo ago
My PR is specifically for adding async on the generateId function, that function already exists (https://www.better-auth.com/docs/reference/options#advanced)
Geospace
GeospaceOP2mo ago
yeah the async part is what I was stuck on iirc

Did you find this page helpful?