Search code examples
node.jsfirebasefirebase-authenticationfirebase-admin

Code review for Listing all firebase users as a promise


This isnt so much a question more code review on what I have written.

I had a go at converting the firebase list all users code into a promise function. I wanted retrieve an array of all users in one go, rather than using the function callbacks.

Could anyone tell me whether this could break, either from using too much memory or some other problem.

I will leave the user batch param as 1 to show that the async does seem to work.

const userArray = [];

// list all users
const listAllUsers = function (nextPageToken){
  return new Promise((resolve, reject) => {
    admin
      .auth()
      .listUsers(1, nextPageToken)
      .then(async(listUsersResult) => {
        listUsersResult.users.forEach((userRecord) => {
          console.log('user', userRecord.email);
          userArray.push(userRecord.email);
        });
        if (listUsersResult.pageToken) {
          console.log("There is a next page token")
          await listAllUsers(listUsersResult.pageToken);
          resolve();
        } else {
          resolve();
        }
      })
      .catch((error) => {
        console.log('Error listing users:', error);
      });
  });
};

// Start listing users from the beginning, 1000 at a time.
await listAllUsers();
  

Solution

  • Well... the code will use an amount of memory that is at least linear to the number of users. So if you have no limit on the number of users, you will also have no way to predict the amount of memory it needs.

    The question that immediately jumps out at me is why you would need a list of all users? What are you going to do on that entire list, that you can't do on a single page of users?