dynamo
dynamo

Reputation: 309

Cognito - Automatically verifying user on sign up

I am trying to automatically verify users emails when signing them up in my web app which is connected to cognito.

I am using the method to try and do this

adminUpdateUserAttributes

I immediately try to do this after i sign a user up. The user is signed up fine but the users email and phone are not verified in the console

What am i doing wrong in my code?

Here is my code:

$('#signUp').submit(function(event) {
        
        var poolData = {
            UserPoolId : '', // your user pool id here
            ClientId : '' // your app client id here
        };
        var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);

        var attributeList = [];

        var dataEmail = {
            Name: 'email',
            Value: $('input[name=email]').val(),
        };

        var dataPhoneNumber = {
            Name: 'phone_number',
            Value: $('input[name=telephone]').val(),
        };
        
        var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);
        var attributePhoneNumber = new AmazonCognitoIdentity.CognitoUserAttribute(dataPhoneNumber);

        attributeList.push(attributeEmail);
        attributeList.push(attributePhoneNumber);
        
        console.log(attributeList)

        userPool.signUp($('input[name=usernameSignUp]').val(), $('input[name=passwordSignUp]').val(), attributeList, null, function(err, result) {
            if (err) {
                alert(err.message || JSON.stringify(err));
                return;
            }
            console.log('signed up')
            var cognitoUser = result.user;
            console.log('user name is ' + cognitoUser.getUsername());
            updateUserAttribute($('input[name=usernameSignUp]').val())
        });
        
        //updates the new user to have verified phone and email using adminUpdateUserAttributes() -- this isnt working rn

        var cognitoidentityserviceprovider = new AWSCognito.CognitoIdentityServiceProvider();
        
        cognitoidentityserviceprovider.adminUpdateUserAttributes({
          UserAttributes: [{
              Name: 'phone_number_verified',
              Value: 'true'
            }, {
              Name: 'email_verified',
              Value: 'true'
            }
          ],
          UserPoolId: '',
          Username: $('input[name=usernameSignUp]').val()
        }, function(err) {
          if (err) {
            console.log(err, err.stack)
          } else {
            console.log('Success!')
          }
        })
    });```

Upvotes: 0

Views: 1288

Answers (1)

Lucasz
Lucasz

Reputation: 1218

For your usecase you should allow autoVerify when signing up.

This is done in a presignup lambda. https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-sign-up.html

You should create a presignup lambda similar like this:

exports.handler = (event, context, callback) => {

    event.response.autoConfirmUser = true;
    event.response.autoVerifyEmail = true;
    event.response.autoVerifyPhone = true;

    callback(null, event);
};

Upvotes: 1

Related Questions