0

Can any only help me with the above requirement?

We have to update the phone_number attribute in AWS cognito and send a SMS MFA to confirm the mobile number. and also we have to verify the code sent to the user.

Vamshi G
  • 23
  • 1
  • 6

2 Answers2

2

Yes ofcourse. You need to use AWS SDK.

const AWS = require('aws-sdk');
const config = require('./config'); 

function updateAttribute(params) {
    AWS.config.update({
        'region' : config.AWSConfig.region,
        'accessKeyId': config.AWSConfig.accessKeyId,
        'secretAccessKey': config.AWSConfig.secretAccessKey
    });
    let cognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider();

    let parameters = { UserPoolId : config.userPoolDetails.userPoolId,
    Username : params.userName,
    UserAttributes : [
        {
            'Name': params.nameOfAttribute ,
            'Value': params.newValueOfAttribute
        },
    ]}
    
    cognitoIdentityServiceProvider.adminUpdateUserAttributes(parameters,function (err, result) {
        if(err)
        console.log(err);
        else
        console.log("Attribute updated successfully");
    })
}

let params = {
    userName : 'username',
    nameOfAttribute : 'name',
    newValueOfAttribute : 'Sachin'
}

updateAttribute(params);

You can even add new attribute like this.

You can read more here : https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminUpdateUserAttributes.html

Anmol Jain
  • 336
  • 4
  • 12
0

Cognito does this automatically if you have phone verification enabled in settings. Just run the UpdateUserAttributes function and set a new phone number.

https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_UpdateUserAttributes.html

Ninad Gaikwad
  • 4,272
  • 2
  • 13
  • 23