Getting all of users from Firebase Auth with pagination

With hundreds of attempts, i founded the right way (based in this solution).
I added two params in success trigger to prevent infinite loading users loop.
image

And just a little modify getAllUsersCode: add if statement to check are there some users in data:

if(data.users.length !== 0) allUsers.setIn([i], data.users)

Full code:

//https://firebase.google.com/docs/auth/admin/manage-users#list_all_users
//firebase auth's list option has a max return of 1000 users. If we want all of them, we'll have to loop through and trigger the query again if we get a pageToken back letting us know we haven't gotten them all yet.

allUsers.setValue([]); //the value of our temp state for storing our baches of users. This must be an array

function next(page, i) {
  getUsers.trigger({
    additionalScope: !!page ? { page: page } : {}, //if we have a token, pass it in the additionalScope
    onSuccess: function (data) {
      if(data.users.length !== 0) allUsers.setIn([i], data.users)
      //onSuccess, save the data into an array in a tempstate at a specific index. 
      if (!!data.pageToken) {
        //if this isn't the final request 
        console.log(data.pageToken);
        next(data.pageToken, i + 1); //re-run getUsers with the pageToken we got, and i+1 to save the next return at in our allUsers temp state
      }
    }
  });
}

next(undefined, 0); //trigger the query the first time, passing in an undefined pageToken

P.S: If this post, see'll someone from Retool Product team, please, improve user experience for standard Firebase modules, such as pagination.

Thank you :blush:

1 Like