Search code examples
node.jsfirebase-authenticationgoogle-cloud-functionsfirebase-admin

Firebase cloud function Admin SDK - listing user - problem with pagination


I've implemented a HTTP callable function to get all users. As you see in my code, the problem is that requestedPageToken (from client call) is the same as listUsersResult.pageToken (returned from listusers function). My client app calls this function with the same token over and over again but it gets only the first page of users because listUsersResult returns the same token and not the next page token. Here is my TypeScript code:

export const listAllUsers = functions.https.onCall((data, context) => {
    try {
      if (data !== null) {
        const requestedPageToken = data.text;
        console.log("PageToken is: " + data);
        return admin.auth().listUsers(50, requestedPageToken)
            .then((listUsersResult) => {
              console.log("New PageToken is: " + listUsersResult.pageToken);
              return listUsersResult;
            }).catch((reason) => {
              return Promise.reject(
                  new Error(reason));
            });
      } else {
        return admin.auth().listUsers(50);
      }
    } catch (error) {
      console.error("Error: " + error);
      return Promise.reject(error);
    }
});

I could not find where the problem is. Why am I not getting the token for next 50 users?


Solution

  • Ok, it works now. I've made a mistake. Below line:

    const requestedPageToken = data.text;
    

    should be:

    const requestedPageToken = data;
    

    I'm not sure how this change affects what the function returned but none the less it works now.