AWS Cognito:如何允许用户无需发送验证码即可更改电子邮件?

时间:2019-07-15 12:17:28

标签: amazon-web-services amazon-cognito

在我的Android应用中,我希望我的用户能够更改其电子邮件地址(用于连接其帐户),而无需通过电子邮件获取任何验证码。

到目前为止,我设法更改了电子邮件地址,并且由于使用了lambda,因此自动将email_verified设置为true。但是不幸的是,电子邮件仍然会发送带有验证码的邮件...

这是我在Android应用程序中所做的:

public void onClickChangeEmail(View view)
{
    CognitoUserAttributes attributes = new CognitoUserAttributes();
    attributes.getAttributes().put("email", "second@mail.com");
    CognitoSettings
            .getCognitoUserPool(MainActivity.this)
            .getCurrentUser()
            .updateAttributesInBackground(attributes, new UpdateAttributesHandler()
    {
        @Override
        public void onSuccess(List<CognitoUserCodeDeliveryDetails> attributesVerificationList)
        {
            Log.i("tag", "Email updated!");
        }

        @Override
        public void onFailure(Exception e)
        {
            e.printStackTrace();
        }
    });
}

在我的AWS控制台中,我在自定义消息的Cognito中添加了一个触发器,这是我的lambda函数,该函数在用户每次更新电子邮件时都会触发:

const AWS = require('aws-sdk')
AWS.config.update({region: 'eu-central-1'});

exports.handler = (event, context, callback) => {
    if (event.triggerSource === 'CustomMessage_UpdateUserAttribute')
    {
        const params = {
            UserAttributes: [
              {
                  Name: 'email_verified',
                  Value: 'true',
              },
            ],
            UserPoolId: event.userPoolId,
            Username: event.userName,
        };
        var cognitoIdServiceProvider = new AWS.CognitoIdentityServiceProvider();
        cognitoIdServiceProvider.adminUpdateUserAttributes(params, function(err, data) {
            if (err) context.done(err, event); // an error occurred
            else context.done(null, event); // successful response
        });
    }
    else
    {
        context.done(null, event);
    }
};

我发现的唯一解决方法是抛出错误而不是context.done(null, event);,但这看起来不是一个干净的解决方案。

是否有更好和更清洁的方法来阻止Cognito发送验证电子邮件?

感谢您的帮助。

1 个答案:

答案 0 :(得分:5)

我正在Springboot服务中调用Cognito API,并且能够在不获取验证码的情况下更新用户的电子邮件。在我的adminUpdateUserAttributes()方法中,我传入:

名称:“ email_verified”, 值:“ true”

连同需要更新的电子邮件字段一起,它成功更新而无需发送电子邮件。也许拉达无法正常工作,或者由于这是一个老问题,所以他们已修复了该错误。