Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any documentation on how to use the Token for pagination (Admin Actions API)? #8362

Closed
hrqmonteiro opened this issue Oct 5, 2021 · 3 comments
Labels
auth Issues tied to the auth category of the CLI documentation Add or update documentation question General question

Comments

@hrqmonteiro
Copy link

hrqmonteiro commented Oct 5, 2021

Which Category is your question related to?

Questions

Amplify CLI Version

6.1.0

What AWS Services are you utilizing?

Cognito, Amplify

Provide additional details e.g. code snippets

I am following this tutorial: https://docs.amplify.aws/cli/auth/admin/#enable-admin-queries
For using the API requests related to the listUsers

And i did it, like this:

let nextToken

async function listUsers(limit: number) {
    const apiName = 'AdminQueries'
    const path = '/listUsers'
    const myInit = {
      queryStringParameters: {
        limit: limit,
        token: nextToken,
      },
      headers: {
        'Content-Type': 'application/json',
        Authorization: `${(await Auth.currentSession())
          .getAccessToken()
          .getJwtToken()}`,
      },
    }
    const { NextToken, ...rest } = await API.get(apiName, path, myInit)
    nextToken = NextToken
    return rest
  }

And, okay, it is working. I can list 60 users in my Cognito user pool. But there's no documentation or tutorial on how to use the pagination queries.
How can i list the next users on it? I can't do it and theres no clues for it.

@hrqmonteiro hrqmonteiro added the question General question label Oct 5, 2021
@yuth yuth added auth Issues tied to the auth category of the CLI documentation Add or update documentation labels Oct 5, 2021
@yuth
Copy link
Contributor

yuth commented Oct 5, 2021

The above code should get second page when you call the listUser second time as the nextToken is set from the first request. If you want to load all the user in a loop you could try something like this.

async function listAllUsers() {
  let nextToken;
  const results = [];
  const apiName = "AdminQueries";
  const path = "/listUsers";
  do {
    const myInit = {
      queryStringParameters: {
        limit: 100,
        token: nextToken,
      },
      headers: {
        "Content-Type": "application/json",
        Authorization: `${(await Auth.currentSession())
          .getAccessToken()
          .getJwtToken()}`,
      },
    };
    const { NextToken, Users } = await API.get(apiName, path, myInit);
    nextToken = NextToken;
    results.push(...Users);
  } while (nextToken);

  return results;
}

PS: I would not recommend loading all users at once. The above code is not optimized and is for demonstration purpose.

@hrqmonteiro
Copy link
Author

The above code should get second page when you call the listUser second time as the nextToken is set from the first request. If you want to load all the user in a loop you could try something like this.

async function listAllUsers() {
  let nextToken;
  const results = [];
  const apiName = "AdminQueries";
  const path = "/listUsers";
  do {
    const myInit = {
      queryStringParameters: {
        limit: 100,
        token: nextToken,
      },
      headers: {
        "Content-Type": "application/json",
        Authorization: `${(await Auth.currentSession())
          .getAccessToken()
          .getJwtToken()}`,
      },
    };
    const { NextToken, Users } = await API.get(apiName, path, myInit);
    nextToken = NextToken;
    results.push(...Users);
  } while (nextToken);

  return results;
}

PS: I would not recommend loading all users at once. The above code is not optimized and is for demonstration purpose.

That didn't work, man. Any other suggestions.
And it is no problem to load all users at once, cause i have an elasticsearch for that, and the max numbers of my users won't be that much.

@github-actions
Copy link

github-actions bot commented Dec 8, 2021

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
auth Issues tied to the auth category of the CLI documentation Add or update documentation question General question
Projects
None yet
Development

No branches or pull requests

2 participants