How to Register with Only Username, Email, and Password?

Hey guys, how’s it going? I need some help. I’m working on a registration form and I just want to use a username, email, and password—but I can’t get it to work properly.
The "name" field is always required, so I was thinking of just using it to store the username. It’s kinda weird, but it could work.
The problem is, I’m not sure how to tweak the registration logic to check if the username (which would be stored in the "name" field) is unique.
I also want to add another field on the backend to store a normalized version of the username, but I have no idea how to modify BetterAuth’s registration logic to make that happen.
Right now, I’m only doing frontend validation with Zod and some basic checks in auth.ts, like setting minPasswordLength and maxPasswordLength.
Any tips on how to make this work? Appreciate it!
Solution:
The name field is for Display names, I think plans are to make it optional in the future but not currently possible. Use the username plugin check docs here https://www.better-auth.com/docs/plugins/username#signup-with-username Also if you dont need the name just set to ''...
Username | Better Auth
Username plugin
Jump to solution
3 Replies
Solution
lonelyplanet
lonelyplanet6mo ago
The name field is for Display names, I think plans are to make it optional in the future but not currently possible. Use the username plugin check docs here https://www.better-auth.com/docs/plugins/username#signup-with-username Also if you dont need the name just set to ''
Username | Better Auth
Username plugin
lonelyplanet
lonelyplanet6mo ago
@Hyper
Hyper
HyperOP6mo ago
For sure! I think I got it! In the auth.ts file, I added this:
user: {
additionalFields: {
name: {
type: "string",
required: false,
},
},
},
user: {
additionalFields: {
name: {
type: "string",
required: false,
},
},
},
Not sure if it actually had any effect yet, but I’ll test it...
Also, as you recommended, I added the username plugin, imported it, and set it up in both auth.ts and auth-client.ts, following the docs.
When calling the signup function, I just passed an empty string for the name:
await authClient.signUp.email({
email: values.email,
password: values.password,
name: "",
username: values.username,
});
await authClient.signUp.email({
email: values.email,
password: values.password,
name: "",
username: values.username,
});
And apparently it worked! The user has been created and the username fields and their normalized version are working as expected!
For now, this solves my issue, though I still have to deal with the name field in the database.
I hope that in the future, just as you said, they make it optional... And about the other question, is there a way for me to modify the registration form logic? I thought the username plugin would require me to use a username for registration and login, and that it wouldn’t be possible to require an email for sign-up... But when you sent me the documentation link, I took a closer look, and sure enough, right at the beginning, there was an example showing how to set it up with an email:
const data = await authClient.signUp.email({
email: "email@domain.com",
name: "Test User",
password: "password1234",
username: "test"
});
const data = await authClient.signUp.email({
email: "email@domain.com",
name: "Test User",
password: "password1234",
username: "test"
});
@lonelyplanet, really appreciate the help!

Did you find this page helpful?