【问题标题】:AWS Cognito: how to allow users to change email without sending verification code?AWS Cognito:如何允许用户在不发送验证码的情况下更改电子邮件?
【发布时间】:2019-11-24 03:34:09
【问题描述】:

在我的 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 发送验证电子邮件?

感谢您的帮助。

【问题讨论】:

    标签: amazon-web-services amazon-cognito


    【解决方案1】:

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

    名称:'email_verified', 值:'真'

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

    【讨论】:

      猜你喜欢
      • 2019-11-23
      • 2020-10-28
      • 2011-12-29
      • 2020-09-13
      • 2018-05-01
      • 2020-03-12
      • 2018-03-02
      • 1970-01-01
      • 2014-01-01
      相关资源
      最近更新 更多