Search code examples
node.jsfirebase-authenticationfirebase-admin

How do I getuser by displayName using firebase admin?


I'm trying to check if the username already exists during the registration process when using firebase-admin. It doesn't seem like there is a getuserbydisplayname function. I tried using the getUser function and passing in a displayName like so, but it doesn't accept that.

        admin.auth().getUsers([{ 
                displayName: username 
            },]).then((getUsersResult) => {
                if(getUsersResult.users.length > 0){
                    console.log("User already exists.");
            }
        })
        .catch((error) => {
          console.log('Error fetching user data:', error);
        });

The documentation says that only uid, email, or phone is accepted. It seems like the only other way would be to list all users and check if any of them have the same displayname, but that seems to be really inefficient. I could also make another database and reference that as well, but it also seems inefficient. How should I best do this?


Solution

  • There is no way to access users by their display name in the Firebase Admin SDK (nor in the other SDKs). You will have to loop over all users, and check their display names individually until you find the one you want.

    Alternatively (and more commonly) you can maintain your own database of user profile information, and perform the query there.