Vamshi G
Vamshi G

Reputation: 35

update a user phone number attribute to AWS cognito and verify phone number through sms mfa using node.js

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.

Upvotes: 1

Views: 4724

Answers (2)

Anmol Jain
Anmol Jain

Reputation: 411

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

Upvotes: 2

Ninad Gaikwad
Ninad Gaikwad

Reputation: 4480

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

Upvotes: 0

Related Questions