Populate username field with social usernames
Using the username plugin. How would I make the social links populate the
username
field? It seems standard behaviour is that Twitch is populating the name
field with the Twitch username, and Discord is populating name
with their actual first name.
I would like so you log in with a social link and your username is set to your Twitch/Discord username.
@Ping Any insight? Thanks in advance10 Replies
You can use
mapProfileToUser
.
https://www.better-auth.com/docs/concepts/oauth#other-provider-configurationsOAuth | Better Auth
How Better Auth handles OAuth
Thank you very much!
Just a couple follow ups on this, not seeing it mentioned on those docs
1. Do you know if there's a way to stop a field mapping? For eg, the "name" field is mapping. I tried setting it to undefined and null, neither stopped it.
name
has no purpose in my project
2. It's a little sticky with username and displayUsername. The username plugin from my understanding has username sanitised to be recognised by system, and displayUsername as the original input. What's the best way for me to map this? I'd need to understand what sanitization is done I think, unless there is a function for thisname
is a requirement in Better Auth. In the future this will be customizable.
username
is more system friendly, yeah. The DisplayName could be thought of as a public-facing name a user may want to allow others to see. For example, a user may have their name
set, but don't want to share that publically, instead they can use the displayName
.
There isn't any limitations to display name, you can set either however you want. Except username will be automatically sanitised. (If I remember correctly)Thanks! Lastly, I presume I am responsible for handling the uniqueness checks here? I see that it's automatically handled in the client.signUp.email, but it doesn't appear to be here. It's letting me create an account with a username that's already taken so long as I map it in here
Better auth would check if username is already taken
I had assumed so, but I'm not seeing this behaviour:
I created an account with that username first, and then signed up with Discord. It doesn't seem to have flagged the duplicate name.
Calling here:
Mapper:
Perhaps I am approaching it wrong?
Here's the data:

I presume I am probably just losing the plot.
Oh I see! You're using mapProfileToUser, right of course.
Yeah you'll have to run this check yourself.
I thought you were using the
signUp.username
method.No that's all good, thank you for the clarity!