AWS Cognito:如何允许用户在不发送验证码的情况下更改电子邮件?

mat*_*eoh 9 amazon-web-services amazon-cognito

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

到目前为止,我设法更改了电子邮件地址,感谢 lambda,设置email_verifiedtrue自动。但不幸的是,仍然发送了一封带有验证码的电子邮件......

这是我在我的 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();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

在我的 AWS 控制台中,我在 Cognito 中的Custom message上添加了一个触发器,这是我的 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);
    }
};
Run Code Online (Sandbox Code Playgroud)

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

有没有更好更干净的方法来防止 Cognito 发送验证电子邮件?

谢谢你的帮助。

小智 6

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

名称:'email_verified',值:'true'

连同需要更新的电子邮件字段,并且无需发送电子邮件即可成功更新。也许 labda 不能正常工作,或者他们已经修复了错误,因为这是一个老问题。