Retrieving a list of users who have registered using Firebase Auth

前端 未结 5 1170
生来不讨喜
生来不讨喜 2020-12-01 22:31

I am making an android chat application using firebase. So far I have made a common group chat app. Now I want to make it an individual chat app, which will have a Users lis

相关标签:
5条回答
  • 2020-12-01 22:41

    If you need to lookup users by uid, email or phoneNumber, you can use the Admin SDK to do so:

    https://firebase.google.com/docs/auth/admin/manage-users

    You also even have the ability to download all your users: https://firebase.google.com/docs/auth/admin/manage-users#list_all_users

    You would need to do that from a Node.js backend server or via HTTP endpoints with Firebase Functions.

    In addition the Admin SDK allows you to set custom user attributes which could be helpful if you want to create different user groups:

    https://firebase.google.com/docs/auth/admin/custom-claims

    admin.auth().setCustomUserClaims(uid, {groupId: '1234'})

    0 讨论(0)
  • 2020-12-01 22:44

    As pointed out by @Sam earlier, you can fetch details from Firebase DB. So every time a user signs up, add his FirebaseUser details (preferably his UID, under which you store his other details if required) to the DB. Then simply put a listener on the Database in your next activity, and you can fetch a list of all registered users.

    0 讨论(0)
  • 2020-12-01 22:55

    The Firebase Admin SDK allows retrieving the entire list of users in batches:

    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();
    

    Note: This API is currently only available for the Admin Node.js SDK.

    via https://firebase.google.com/docs/auth/admin/manage-users

    0 讨论(0)
  • 2020-12-01 23:01

    You cannot retrieve the data of all authenticated user from Firebase Authentication, however you can only get the current user.
    In Order to get the data of registered user you need to store it into database and then retrieve the whole array, or you can just keep the authentication flag which can be set or reset if the user is registered in your all-user table and vice versa.

    As mentioned by @Jason you can try Admin SDK as it is mentioned in the documentation that listAllUsers() can retrieve batch of user data.
    The detailed explanation can be found IN THIS THREAD.

    0 讨论(0)
  • 2020-12-01 23:04
    const allUsers: firebase.auth.UserRecord[] = [];
    
    const listAllUsers = async (nextPageToken?: string) => {
      const res = await firebase.auth().listUsers(1000, nextPageToken);
      allUsers.push(...res.users);
      if (res.pageToken) {
        await listAllUsers(res.pageToken);
      }
    };
    
    await listAllUsers();
    
    console.log(allUsers)
    
    0 讨论(0)
提交回复
热议问题