AWS Cognito: Methods to Get a List of users?

Viewed 19672

I'm developing a web application using Angular 4 (with TypeScript language) front-end side, and using AWS services back-end size. This application can be only accessed by a group of users (each has its own mail and password). This group of users is defined in AWS Cognito - User Pool. How can I have the entire list of these users with their properties to see her in the frontend? And how do I change their properties only frontend side?

I saw the JavaScript methods for AWS Cognito (https://github.com/aws/amazon-cognito-identity-js), but I didn't find anything for what I want to do.

I would like to use some method to show the full list of users with their properties (as seen in the AWS mangement console of Cognito) and that always, thanks to another method, my application it's able to edit some information as a "group" of a user.

Can anyone tell me if this is possible?

4 Answers

This function will list the users, just use the aws key and secret, user pool region and id and call the function getUsers(). You can use filters in params to do a more specific request. https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#listUsers-property

var AWS = require('aws-sdk');

exports.getUsers= () => {
var params = {
  UserPoolId: USER_POOL_ID
  AttributesToGet: [
    'ATTRIBUTE_NAME',
  ],
};

return new Promise((resolve, reject) => {
    AWS.config.update({ region: USER_POOL_REGION, 'accessKeyId': AWS_ACCESS_KEY_ID, 'secretAccessKey': AWS_SECRET_KEY });
    var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
    cognitoidentityserviceprovider.listUsers(params, (err, data) => {
        if (err) {
            console.log(err);
            reject(err)
        }
        else {
            console.log("data", data);
            resolve(data)
        }
    })
});

}

Late response but for those who will probably need it :

  1. Create a new IAM policy, with the Cognito Listusers allowed

  2. the code in JS

    const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({
         apiVersion: '2016-04-18',
         region: "eu-central-1",
         credentials: Auth.essentialCredentials(credentials)
       });
       var params = {
         UserPoolId: environment.amplify.Auth.userPoolId, /* required */
         AttributesToGet: [
           "email",
         ],
         Limit: 0
       };
       cognitoidentityserviceprovider.listUsers(params, function (err, data) {
         if (err) console.log(err, err.stack); // an error occurred
         else console.log(data);           // successful response
       });
     });
    

Look into the Cognito Identity Service Provider SDK.

SDK Link

The function that want is listUsers();

This is the cleanest way I've found so far in Typescript.

Calling .promise() after listUsers, we can wrap a try catch block around our API call.

import { Result } from '@badrap/result'
import { CognitoIdentityServiceProvider } from 'aws-sdk'
import {
  ListUsersRequest,
  ListUsersResponse,
} from 'aws-sdk/clients/cognitoidentityserviceprovider'

export const listUsers = async (): Promise<Result<ListUsersResponse>> => {
  const identityProvider = new CognitoIdentityServiceProvider({
    region: "<your_region>",
    endpoint: "<your_endpoint>",
  })

  const params: ListUsersRequest = {
    UserPoolId: "<your_pool_id>",
  }

  let result: ListUsersResponse = {}

  try {
    result = await identityProvider.listUsers(params).promise()
  } catch (error) {
    return Result.err(error)
  }

  return Result.ok(result)
}
Related