Amplify Cognito Admin Query listUsersInGroup not working

325 views Asked by At

I am working with AWS Amplify, specifically with Auth; I am trying to get the listUsersInGroup from Cognito pool using the next function in React:

import { Auth, API } from 'aws-amplify';

const listUsersInGroup = async (group) => {
    let apiName = 'AdminQueries';
    let path = '/listUsersInGroup';
    let myInit = {
      body: {
        "groupname": group
      }, 
      headers: {
        'Content-Type' : 'application/json',
        Authorization: `${(await Auth.currentSession()).getAccessToken().getJwtToken()}`
      } 
    };    
    const supervisorGet = await API.get(apiName, path, myInit);
  }

But the API response is having code 403 and the response brings the message: {"message":"groupname is required"}

I made tests with other HTTP methods like listUsers, listGroups, listGroupsForUser and works correctly.

Can anyone help on this issue?

1

There are 1 answers

0
Dave On

I found the example below in the Amplify documenation for admin actions.

It looks like the myInit object you are making is using body, but the example has queryStringParameters. That's why it's not finding the groupname key.

let nextToken;

async function listEditors(limit){
  let apiName = 'AdminQueries';
  let path = '/listUsersInGroup';
  let myInit = { 
      queryStringParameters: {
        "groupname": "Editors",
        "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;
}