Cognito 中的预注册 lambda 函数

问题描述

在创建新的 Cognito 用户时,我想通过 Pre Sign-up lambda 确认他们的帐户。它看起来如下:

exports.handler = (event,context,callback) => {

    // Confirm the user
        event.response.autoConfirmUser = true;

    // Set the email as verified if it is in the request
    if (event.request.userAttributes.hasOwnProperty("email")) {
        event.response.autoVerifyEmail = true;
    }

    // Set the phone number as verified if it is in the request
    if (event.request.userAttributes.hasOwnProperty("phone_number")) {
        event.response.autoVerifyPhone = true;
    }

  console.info("EVENT\n" + JSON.stringify(event,null,2))
  console.info("CONTEXT\n" + JSON.stringify(context,2))
  
    // Return to Amazon Cognito
    callback(null,event);
};

我收到的云监控日志

"response": {
    "autoConfirmUser": true,"autoVerifyEmail": true,"autoVerifyPhone": true
}

但在 cognito 中,用户未验证 phone_number_verified=false、email_verified=false 且用户状态为 FORCE_CHANGE_PASSWORD 而不是 CONFIRMED

解决方法

当通过 API 调用完成新用户的注册时,Cognito 确实会忽略预签名 lambda。即使 lambda 被调用,Cognito 也会忽略响应。如果不应该忽略 lambda 结果,则通过 UI 注册是目前唯一的选择。