0

Is there a way to get user details (profile attributes etc) if I have IdentityPool or UserPool ID (sub) of a user with AWS SDK?

The use case is that I'm saving some information submitted by a user in a DB with a key equal to user ID (sub). So, when I'm reading it from the DB, I want to restore back some user info from my pool for my app UI.

I found a similar question (Getting cognito user pool username from cognito identity pool identityId), but it seems, the answer given is focused around serverless deployment, and still has some gaps.

Thanks in advance

Philipp Grigoryev
  • 1,985
  • 3
  • 17
  • 23

2 Answers2

1

Since you have the user's sub, you can use AdminGetUser. It returns the UserAttributes in the pool.

Ulas Keles
  • 1,681
  • 16
  • 20
0

I think I found a solution, it was on the surface actually.

Having user pool id one can use ListUsers call with filter sub = \"${userId}\". The client to be used is CognitoIdentityProviderClient, if JS is used.

    const client = new CognitoIdentityProviderClient({
        region: REGION,
        credentials: fromCognitoIdentityPool({
            client: new CognitoIdentityClient({ region: REGION }),
            logins: {
                [PROVIDER_ID]: token
            },
            identityPoolId: ID_POOL_ID
        })
    });

    const filter = `sub = \"${userPoolId}\"`;

    const resp = await client.send(new ListUsersCommand({
        UserPoolId: USER_POOL_ID,
        Filter: filter,
        Limit: 1
    }));

Of course AdminGetUser can be used as well, as Ulas Keles mentioned above, if it's applicable

Philipp Grigoryev
  • 1,985
  • 3
  • 17
  • 23