Unique usernames but not unique emails
-
We have a question about user uniqueness.
We want to have multiple users with unique usernames, but duplicate email addresses.
When creating some users, we found FusionAuth wants unique usernames and email addresses. Is there a way to have usernames be unique, but not emails?
-
The recommended course of action is to store the email in the
user.data.email
field, which is not required to be unique but is still used for activities like 'forgot password' emails. Leave theuser.email
field blank and useuser.username
.That should work. Support for
user.data.email
was removed in 1.26 and added back in 1.27.2, so avoid version 1.26.* and 1.27.0 and 1.27.1. -
-
Hey @dan,
Would you mind if I ask you to differentiate between
uniqueUsername
andusername
both inupdateUser
andregister
in FusionAuth Typescript Client?await fusionAuthClient.updateUser(id, { // ... user: { uniqueUsername: username, }, }); await fusionAuthClient.updateUser(id, { // ... user: { username: username, }, }); await fusionAuthClient.register('', { // ... user: { username, }, }); await fusionAuthClient.register('', { // ... user: { uniqueUsername, }, });
Questions
- If I use
username
instead ofuniqueUsername
, then should I enforce uniqueness manually in my NestJS app? - If I use
uniqueUsername
then will I have access to it aspreferred_username
returned by FusionAuth?
- If I use
-
I tried to specify both username and email and I got this error:
{ 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: [] } }
So basically I guess my best bet is to manually enforce uniqueness of username in my backend. But it could have been less cumbersome if I could delegate it to FusionAuth.
I guess what I am trying to emphasis here is the fact that just by saving
username
inuser.data
we will be able to have both username and email but applying rules such as uniqueness would require more manual labor. Cannot we just tell FusionAuth to make sure thatuser.data.username
should be unique in that app or tenant?Side note: Since my question is deviating from the OP I'll create a new post and reference this one.