简体   繁体   English

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

[英]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.

相关问题 如何使用 firebase admin sdk 列出所有用户 - How can I list all users using firebase admin sdk 如何使用 Node.js 和“firebase-admin”SDK 查询 Firebase? - How to query Firebase with Node.js and "firebase-admin" SDK? 如何使用 Java 中的 Firebase Admin 将文件上传到 Firebase 存储? - How to Upload file to Firebase Storage using Firebase Admin in Java? 在 firebase_admin 上使用“where”查询时无法过滤数据 - cannot filter data when using `where` query on firebase_admin 如何使用带有 get() 的 firebase_admin 查询 firestore 文档 - How do I query a firestore document using firebase_admin with get() 如何使用 firebase admin SDK 访问 admin firestore? - How to access admin firestore using firebase admin SDK? 如何通过 Firebase Admin 针对代理连接到 Firebase? - How to connect to Firebase by Firebase Admin against a proxy? 如何搜索用户表单 firebase - How search users form firebase 如何使用 firebase_admin 从 python 中的 firebase 存储中下载文件 - How to download file from firebase storage in python using firebase_admin 我想使用 firebase 为我的仪表板添加基于身份验证的角色,例如管理员和编辑用户 - i want to add auth based roles like admin and editor users for my dashboard using firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM