FusionAuth
    • Home
    • Categories
    • Recent
    • Popular
    • Pricing
    • Contact us
    • Docs
    • Login

    Unique usernames but not unique emails

    Scheduled Pinned Locked Moved
    Q&A
    username email uniqueness
    2
    4
    2.3k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • danD
      dan
      last edited by

      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?

      --
      FusionAuth - Auth for devs, built by devs.
      https://fusionauth.io

      1 Reply Last reply Reply Quote 0
      • danD
        dan
        last edited by

        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 the user.email field blank and use user.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.

        --
        FusionAuth - Auth for devs, built by devs.
        https://fusionauth.io

        K 1 Reply Last reply Reply Quote 0
        • C cedric.baiker referenced this topic on
        • K
          kasir-barati @dan
          last edited by

          Hey @dan,

          Would you mind if I ask you to differentiate between uniqueUsername and username both in updateUser and register 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

          1. If I use username instead of uniqueUsername, then should I enforce uniqueness manually in my NestJS app?
          2. If I use uniqueUsername then will I have access to it as preferred_username returned by FusionAuth?
          K 1 Reply Last reply Reply Quote 0
          • K
            kasir-barati @kasir-barati
            last edited by kasir-barati

            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 in user.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 that user.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.
            1 Reply Last reply Reply Quote 0
            • First post
              Last post