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 advance
10 Replies
Ping
Ping11h ago
Stephen
StephenOP11h ago
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 this
Ping
Ping10h ago
name 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)
Stephen
StephenOP10h ago
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
Ping
Ping10h ago
Better auth would check if username is already taken
Stephen
StephenOP10h ago
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:
await client.signIn.social({
provider,
callbackURL: "/dashboard"
}, {
onError: (ctx) => {
alert(JSON.stringify(ctx))
}
});
await client.signIn.social({
provider,
callbackURL: "/dashboard"
}, {
onError: (ctx) => {
alert(JSON.stringify(ctx))
}
});
Mapper:
discord: {
clientId: DISCORD_CLIENT_ID,
clientSecret: DISCORD_CLIENT_SECRET,
mapProfileToUser: (profile) => {
// Note to self: "name is a requirement in Better Auth. In the future this will be customizable." AKA can't unmap name.
return {
username: profile.username,
displayUsername: profile.username,
};
},
},
discord: {
clientId: DISCORD_CLIENT_ID,
clientSecret: DISCORD_CLIENT_SECRET,
mapProfileToUser: (profile) => {
// Note to self: "name is a requirement in Better Auth. In the future this will be customizable." AKA can't unmap name.
return {
username: profile.username,
displayUsername: profile.username,
};
},
},
Perhaps I am approaching it wrong?
Stephen
StephenOP10h ago
Here's the data:
No description
Stephen
StephenOP10h ago
I presume I am probably just losing the plot.
Ping
Ping10h ago
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.
Stephen
StephenOP10h ago
No that's all good, thank you for the clarity!

Did you find this page helpful?