Unsolved Unique username and email at the same time
-
username
, andemail
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 specifyusername
andemail
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
inuser.data
we will be able to have bothusername
andemail
but applying rules such as uniqueness would require more manual work. Cannot we just tell FusionAuth to make sure thatuser.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.
-
-
It seems like I do not have any mean to implement this in FusionAuth, I search for a way to define new constraints in DB for
user.data
field but no luck.Then the other option is a vulgar way of doing it, fetching all users and then looping over users and comparing the
user.data.username
with the new one. It is not gonna work in a medium-size or large user base app.I can think of one more option which is saving all usernames in my MongoDB database which can work but is not what I would volunteer for it.
Any suggestion?
-
I can also think of one ugly solution which is not very pleasant for the users and that would be generating usernames by concatenating
firstName
,lastName
and a random hexadecimal string.