0

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Is your log message correctly printing? – samthecodingman Dec 22 '21 at 10:20
  • Yes, here is the output from logs of these two log messages: `PageToken is: 2Sq0O25MHMgXdcQMz5M0...` and here is the other one `New PageToken is: 2Sq0O25MHMgXdcQMz5M0...` – Adam Sowiński Dec 22 '21 at 12:09
  • Sweet, so the problem was exactly what I thought it was: `data` is a string, `data.text` will return `undefined` (because strings don't have a text property), which means that you effectively call `admin.auth().listUsers(50, undefined)` which is the same as `admin.auth().listUsers(50)` - so no token, same results. – samthecodingman Dec 22 '21 at 22:08

1 Answers1

0

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.