AWS Cognito 与 Lambda 用于自定义验证

AWS Cognito with Lambda for custom validation

将 AWS Lambda 与 Cognito 结合使用,我们可以使用以下代码自动验证电子邮件。

event.response.autoConfirmUser = true;
event.response.autoVerifyEmail = true;

如何在此处进行自定义请求验证?

例如,如果我想在 Cognito 注册时发送促销代码,那么我能否有一个代码来验证此促销代码并在促销代码无效时拒绝注册请求。

您可以设置触发器来自定义 UserPool 工作流程:http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html

对于您的情况,设置预注册触发器应该没问题。

成功了:-)

exports.handler = (event, context, callback) => {
    //Auto confirming user and verifying emaail
    event.response.autoConfirmUser = true;
    event.response.autoVerifyEmail = true;
    //Extract Registration code from user attributes 
    var rCode = event.request.userAttributes["custom:rCode"];
    var validRCode = "abcdef";
    if (rCode && rCode.toLowerCase() != validRCode) {
         //If registration code is available and it is not equal to validRCode then throw error message
         var error = new Error(': Invalid registration code used.');
         context.done(error, event);
    } else {
      context.done(null, event);
    }
};