繁体   English   中英

如何使用Firebase admin查询Firebase用户?

[英]How to query Firebase users using Firebase admin?

我需要使用 firebase 管理员查询我的用户。 有一种方法可以列出所有用户,但行不通。 我想查询用户。 例如,检索显示名称中包含peter的所有用户。 我怎样才能做到这一点? 用户表不在 firebase 数据库中。

按照新的Admin SDK,您可以像这样从Firebase数据库中获取所有用户。 官方链接-> 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();

对于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);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM