Rango527
Rango527

Reputation: 221

Can change the user's email in aws cognito user pool?

I am using aws cognito user pool in my application and users can log in to the app using their email that verified in aws cognito. Users can change login email and the users must verify the new email. But users can't change login email in my application now, and I don't know how to solve it. We need to find a solution to update the email on the AWS cognito and fix it. How can change the user's email in aws cognito user pool?

Upvotes: 15

Views: 19488

Answers (2)

joe06
joe06

Reputation: 458

You can use UpdateUserAttribute to update the email

 const params = {
        AccessToken: accessToken, /* required */
        UserAttributes: [ /* required */
            {
                Name: 'email', /* required */
                Value: email
            }
       }; 

        const response = await cognito.updateUserAttributes(params).promise();

later, you will need to confirm this change using this method VerifyUserAttribute

const params = {
        AccessToken: accessToken, /* required */
        AttributeName: attributeName, /* required */
        Code: code /* required */
    };
    try {
        const response = await cognito.verifyUserAttribute(params).promise();

This is the best way to change email or some others attributes

Upvotes: 4

Huy Chau
Huy Chau

Reputation: 2240

You can use adminUpdateUserAttributes to update user email and email_verified after that Amazon Cognito sends email again (check here).

const params = {
  UserPoolId: 'UserPoolID',
  Username: 'username',
  UserAttributes: [
    {
      Name: "email",
      Value: "new email"
    },
    {
      Name: "email_verified",
      Value: "false"
    }
  ],
};
const cognitoClient = new AWS.CognitoIdentityServiceProvider();
const createPromise = cognitoClient.adminUpdateUserAttributes(params).promise();
await createPromise;

Upvotes: 9

Related Questions