Cognito - Checking Existing User

0

I have created user pool and adding users to the pool by logging in through the AWS console. When I add a user to the pool, the confirmation status set to "force change password". And an email is get sent to the user email address with a temporary password.

I have built a ReactJS application where I want the user to set their own password. And I intend to use the following workflow in setting their own password.

1.User is on the forgot password page --> 2.user enters the email address --> 3.if user exists, an email is sent to the email address with unique code --> 4.simultaneously user redirected to a page, where user can enter the code and new password.

In the 2 step, where user enters the email address, I have used the following to code to validate if the user exists in the user pool.

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


export  const getUser = (email) => {
    

    return new Promise( async (resolve, reject) => {

        const input = {
            UserPoolId: UserPool.getUserPoolId(),
            Username: email
        };
        
        const client = new CognitoIdentityProviderClient({
            region: region_name_here
        });
       
        const command = new AdminGetUserCommand(input);
        
        const response = await client.send(command);


    });
}

But this piece of code throughs an error. Specifically in the "CognitoIdentityProviderClient", which says "credentials are missing". Can someone let me know what I am doing wrong in here please ? And I need to know how I can use that temp password and set an own password for the users.

champer
asked 9 months ago646 views
1 Answer
0
Accepted Answer

Hi,

Any service API prefixed by Admin like AdminGetUserCommand requires AWS IAM credentials to be executed, see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminGetUser.html

Also, I would recommend using the ListUsers command instead, which still requires AWS IAM credentials, that won't return an error code if not present but a void array. See https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html

Jeff

AWS
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions