簡體   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