Search code examples
firebasefirebase-authenticationfirebase-admin

Fetch multiple users


I have a list (uid's) of users that is connected to a group. On one page I want to list all usernames and emails of the users in the list.

What is the recommended way to fetch user data for each user? All I can find is in the admin sdk: admin.auth().getUser(uid)

  1. Is there no way to fetch multiple users in one request? ex admin.auth().getUsers([uid1, uid2, uid3])?
  2. Is there no way to do this in the client sdk?
  3. If 1. is not possible, is it not quite easy to reach the limits? https://firebase.google.com/docs/auth/limits#api_limits

Solution

  • It's not possible to retrieve the list of all users with the client SDK, but you can do it in two ways:

    Using the Firebase Realtime Database

    1. You can add a node of user names and emails, say /users in your database:

      {
        "users": {
          "user1": {
            "username": "user1",
            "email": "user1@mail.com"
          },
          "user2": {
            "username": "user2",
            "email": "user2@mail.com"
          }
        }
      }
      
    2. Then just query this node using once('value'):

      const users = await firebase.database.ref('/userProfiles').once('value');
      console.log(users);
      

    Using a callable function and the Firebase Admin SDK

    1. Create an HTTPS callable function, say getAllUsers(), that retrieves all users. The Firebase Admin SDK has a way to list all users in batches of 1000.

    2. Deploy the function using firebase deploy --only functions:getAllUsers.

    3. Then call the function from the client:

      const getAllUsers = firebase.functions().httpsCallable('getAllUsers');
      const users = await getAllUsers();
      console.log(users);