I am using user pool in AWS Cognito for creating users and authentication and authorization for the users of my java spring-boot application and I have written code to get users from Cognito and its working fine but I want to fetch users on pagination way, and Cognito also provides the same but the issue is it does not provides the to and fro pagination like each listuserrequest I am getting page token if there are more users and if page token is received null means its the last chunk of users but consider there are 10 users in my pool and I had set limit, 2 users, in each call and ideally it breaks into 5 pages to get my problem is if I have fetched 3rd page and again want to or get users at 2nd page how can I achieve this, please help if anybody knows about this that how it can be achieved
private List<UserDto> getAllUserPagination() {
final ListUsersRequest listUsersRequest = new ListUsersRequest()
.withUserPoolId(awsConfig.getPoolId())
.withLimit(2);
ListUsersResult listUsersResult = cognitoClient.listUsers(listUsersRequest);
final List<UserDto> users = listUsersResult.getUsers().stream()
.map(UserMapper::toUserModel)
.collect(Collectors.toList());
do {
System.out.println(listUsersResult.getPaginationToken());
listUsersRequest.setPaginationToken(listUsersResult.getPaginationToken());
listUsersResult = cognitoClient.listUsers(listUsersRequest);
users.addAll(listUsersResult.getUsers().stream()
.map(UserMapper::toUserModel)
.collect(Collectors.toList()));
System.out.println(users.toString());
} while ((Objects.nonNull(listUsersResult.getPaginationToken())));
return users;
}