#Can't fetch ListUsers with CognitoIdentityProviderClient (aws-sdk)

1 messages · Page 1 of 1 (latest)

torpid badge
#

Hi everybody, I have a problem fetching users list from Cognito User Pool.

I'm working with next.js with /app router (v14.0.4), aws-amplify (v6.0.10) and @aws-sdk/client-cognito-identity-provider (3.496.0)

I'm trying to create an user page (profile page) and a page of users and my components look like this:

app/(auth)/(users)/[id]/actions.ts


import {
  CognitoIdentityProviderClient,
  ListUsersCommand,
} from "@aws-sdk/client-cognito-identity-provider";

export const listUsers = async () => {
  const cognitoIdentityProviderClient = new CognitoIdentityProviderClient({});
  const userPoolId = process.env.NEXT_PUBLIC_USERPOOL_ID!;

  const params = {
    UserPoolId: userPoolId,
  };

  try {
    const result = await cognitoIdentityProviderClient.send(
      new ListUsersCommand(params)
    );

    return result.Users;
  } catch (err: any) {
    console.error(err, err.stack);
    if (err.digest) {
      console.error("Error Digest:", err.digest);
    }
    throw err;
  }
};

app/(auth)/(users)/[id]/page.tsx


...
const UserPage = () => {
 ...

  useEffect(() => {
    const fetchUsers = async () => {
      try {
        // @ts-ignore
        const usersFromUserPool: UserType[] | undefined = await listUsers();
        setUsers(usersFromUserPool || []);
      } catch (error: any) {
        console.error("Error fetching users:", error);
      }
    };
    fetchUsers();
  }, []);

 ...
  return (
   ..
  );
};

export default UserPage;```

**Everything works fine in local, but not in production**, where I get this error:
```An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.```
I also included the env var in my amplify console and in my build settings.

Any idea what I'm doing wrong?
Thanks in advance

my env vars (see image)
#

For more context this is what my amplify.yml under App settings: Build settings look like:

backend:
  phases:
    build:
      commands:
        - '# Execute Amplify CLI with the helper script'
        - amplifyPush --simple
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - env | grep -e BUCKET_NAME -e BUCKET_REGION -e ACCESS_KEY -e SECRET_ACCESS_KEY -e NEXT_PUBLIC_USERPOOL_ID >> .env.production
        - npm run build
  artifacts:
    baseDirectory: .next
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*
bitter hollow
#

hey @torpid badge, wondering if you found the answer to this? facing the same issue - i am storing the secrets in .env - like you, i am able to access it in Local but not on production.