AWS Cognito:如何允许用户在未经验证的情况下更改电子邮件?

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

我是 AWS 的新手,我正在寻找一种方法,让我的 Android 应用程序的用户无需通过验证过程即可更改他们的电子邮件(我设法为订阅做到了这一点)。

我试图遵循这个这个,这就是我所做的。

在我的 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)

结果是:电子邮件已正确更新(但它可以在没有 lambda 的情况下工作),但 lambda 崩溃,并出现以下错误:

autoValidationUserEmailModification is not authorized to perform: cognito-idp:AdminUpdateUserAttributes

所以看起来缺少授权。

我的问题是:

  • 如何修复授权部分?
  • 这种方法是在更新用户电子邮件时禁用电子邮件验证的正确方法吗?

谢谢你的帮助。

hoa*_*gdv 9

允许您的功能AdminUpdateUserAttributes在您的 Cognito 池资源上执行。

使用块更新 Lambda 执行规则,例如:

{
    "Action": [
        "cognito-idp:AdminUpdateUserAttributes"
    ],
    "Resource": "arn:aws:cognito-idp:eu-central-1:<your-user-id>:userpool/<your-user-pool>",
    "Effect": "Allow"
}
Run Code Online (Sandbox Code Playgroud)

Resource您的 Cognito 用户池 ARN在哪里。