【问题标题】:AWS Cognito: how to allow users to change email without verification?AWS Cognito:如何允许用户在不验证的情况下更改电子邮件?
【发布时间】:2019-11-23 07:55:43
【问题描述】:

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

我尝试关注thisthis,这就是我所做的。

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

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

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

所以看起来好像缺少授权。

我的问题是:

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

感谢您的帮助。

【问题讨论】:

  • autoValidationUserEmailModification 是你的函数名吗?
  • @hoangdv 是的
  • 我刚刚创建了一个答案。

标签: amazon-web-services amazon-cognito


【解决方案1】:

允许您的函数在您的 Cognito 池资源上执行 AdminUpdateUserAttributes

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

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

Resource 是您的 Cognito 用户池 ARN。

【讨论】:

  • 感谢您的帮助,现在可以使用了!也就是说,它仍然通过电子邮件发送验证码,有什么办法可以禁用它?
  • 到目前为止,我发现阻止 Cognito 发送电子邮件的唯一方法是在最后抛出异常(但这不是一个非常干净的解决方案......)。有没有更清洁的方法?
  • @thenaoh 您可以尝试每次更新每个属性。先更新用户邮箱,再更新email_verified属性,最后完成lambda函数。
  • 我不太清楚你的意思。无论如何,我刚刚在这里发布了一个新问题供您参考:stackoverflow.com/questions/57039616/…
猜你喜欢
  • 2019-11-24
  • 2020-10-28
  • 2020-09-13
  • 2018-05-01
  • 2018-03-02
  • 2021-05-01
  • 2013-11-14
  • 2021-03-26
  • 2011-12-29
相关资源
最近更新 更多