'How to query Firebase users using Firebase admin?

I need to query my users using firebase admin. There's a way to list all users but it won't work. I want to query the users. For example, retrieve all users who have peter in his display name. How can I do this? The users table is not in the firebase database.



Solution 1:[1]

For TypeScript I had to modify the sample code listed @ https://firebase.google.com/docs/auth/admin/manage-users#list_all_users to:

const listAllUsers = async (nextPageToken: undefined | string) => {

    await admin.auth().listUsers(1000, nextPageToken).then(async (listUsersResult) => {
      listUsersResult.users.forEach((userRecord) => {
          // do something with userRecord
      });
      
      if (listUsersResult.pageToken) {                   
          await listAllUsers(listUsersResult.pageToken);
      }
    }).catch((error) => {
      functions.logger.info('Error listing users:', error);
    });
    
};
await listAllUsers(undefined);

Additional details in the getUsers method docs:

/**
 * Retrieves a list of users (single batch only) with a size of `maxResults`
 * starting from the offset as specified by `pageToken`. This is used to
 * retrieve all the users of a specified project in batches.
 *
 * See [List all users](/docs/auth/admin/manage-users#list_all_users)
 * for code samples and detailed documentation.
 *
 * @param maxResults The page size, 1000 if undefined. This is also
 *   the maximum allowed limit.
 * @param pageToken The next page token. If not specified, returns
 *   users starting without any offset.
 * @return A promise that resolves with
 *   the current batch of downloaded users and the next page token.
 */
listUsers(maxResults?: number, pageToken?: string): Promise<ListUsersResult>;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 g2server