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
Any tips on how to make this work? Appreciate it!
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:Jump to 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
3 Replies
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
@Hyper
For sure! I think I got it!
In the
Not sure if it actually had any effect yet, but I’ll test it...
Also, as you recommended, I added the
When calling the signup function, I just passed an empty string for the name:
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
@lonelyplanet, really appreciate the help!
auth.ts
file, I added this: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:
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:@lonelyplanet, really appreciate the help!