Reputation: 8172
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.
Upvotes: 0
Views: 1214
Reputation: 5367
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>;
Upvotes: 0
Reputation: 1450
As per the new Admin SDK, you can do like this to fetch all the user from the firebase database. Official link -> https://firebase.google.com/docs/auth/admin/manage-users#list_all_users
function listAllUsers(nextPageToken) {
// List batch of users, 1000 at a time.
admin.auth().listUsers(1000, nextPageToken)
.then(function(listUsersResult) {
listUsersResult.users.forEach(function(userRecord) {
console.log("user", userRecord.toJSON());
});
if (listUsersResult.pageToken) {
// List next batch of users.
listAllUsers(listUsersResult.pageToken)
}
})
.catch(function(error) {
console.log("Error listing users:", error);
});
}
// Start listing users from the beginning, 1000 at a time.
listAllUsers();
Upvotes: 4