username
, and email
uniqueness.
We wanna let users to register with unique usernames, and email addresses. I know that we can store the email/username in the user.data
field, which is not required to be unique. And we know that we cannot specify username
and email
at the same time. Look at this error message you'll get when you try to specify both:
{
statusCode: 400,
exception: {
fieldErrors: {
'user.email': [
{
code: '[blank]user.email',
message: 'You must specify either the [user.email] or [user.username] property. If you are emailing the user you must specify the [user.email].'
}
],
'user.username': [
{
code: '[blank]user.username',
message: 'You must specify either the [user.email] or [user.username] property. If you are emailing the user you must specify the [user.email].'
}
]
},
generalErrors: []
}
}
I was trying to update a user with following code:
await fusionAuthClient.updateUser(id, {
user: {
lastName: updateUserInfoDto.lastName,
firstName: updateUserInfoDto.firstName,
uniqueUsername: updateUserInfoDto.username,
},
});
So here is a similar Q&A: https://fusionauth.io/community/forum/post/3055
Qustion
I am trying to emphasis that just by saving username
in user.data
we will be able to have both username
and email
but applying rules such as uniqueness would require more manual work. Cannot we just tell FusionAuth to make sure that user.data.username
should be unique in that application or tenant?
I really do not like to check it manually in my backend considering how costly and inefficient it would be;
- Send an HTTP req to FusionAuth API to fetch all the users.
- Loop over them to verify
user.data.username
is not equal to the one that is entered by the user.
- Update user.
Or at least that's how I though about its implementation.