[英]How to query Firebase users using Firebase admin?
I need to query my users using firebase admin.我需要使用 firebase 管理员查询我的用户。 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.例如,检索显示名称中包含peter
的所有用户。 How can I do this?我怎样才能做到这一点? The users table is not in the firebase database.用户表不在 firebase 数据库中。
As per the new Admin SDK, you can do like this to fetch all the user from the firebase database. 按照新的Admin SDK,您可以像这样从Firebase数据库中获取所有用户。 Official link -> https://firebase.google.com/docs/auth/admin/manage-users#list_all_users 官方链接-> 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();
For TypeScript
I had to modify the sample code listed @ https://firebase.google.com/docs/auth/admin/manage-users#list_all_users to:对于TypeScript
,我必须将 @ https://firebase.google.com/docs/auth/admin/manage-users#list_all_users中列出的示例代码修改为:
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: getUsers
方法文档中的其他详细信息:
/**
* 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>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.