node.js - 亚马逊认知 : How to implement Post Confirmation Trigger which works only after SignupConfirmation not after ForgotPassword/ResetPassword Confirmation

标签 node.js angular aws-lambda amazon-cognito aws-amplify

有人可以告诉我,如何才能在注册的确认后后发送此邮件。无论是忘记密码/重设密码还是注册,此代码都会在每次确认后发送邮件。

var aws = require('aws-sdk');

var ses = new aws.SES();

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

    if (event.request.userAttributes.email) {
            sendEmail(event.request.userAttributes.email, "Congratulations " + event.userName + ", you have been confirmed: ", function(status) {

            // Return to Amazon Cognito
            callback(null, event);
        });
    } else {
        // Nothing to do, the user's email ID is unknown
        callback(null, event);
    }
};

function sendEmail(to, body, completedCallback) {
    var eParams = {
        Destination: {
            ToAddresses: [to]
        },
        Message: {
            Body: {
                Text: {
                    Data: body
                }
            },
            Subject: {
                Data: "Cognito Identity Provider registration completed"
            }
        },

        // Replace source_email with your SES validated email address
        Source: "<source_email>"
    };

    var email = ses.sendEmail(eParams, function(err, data){
        if (err) {
            console.log(err);
        } else {
            console.log("===EMAIL SENT===");
        }
        completedCallback('Email sent');
    });
    console.log("EMAIL CODE END");
};

最佳答案

您可以检查 event 对象的 triggerSource 属性。

注册后确认事件的触发源是 PostConfirmation_ConfirmSignUp

您的句柄函数将如下所示:

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

  if (
    event.request.userAttributes.email
    && event.triggerSource === "PostConfirmation_ConfirmSignUp") {
    sendEmail(event.request.userAttributes.email, "Congratulations " + event.userName + ", you have been confirmed: ", function (status) {

      // Return to Amazon Cognito
      callback(null, event);
    });
  } else {
    // Nothing to do, the user's email ID is unknown
    callback(null, event);
  }
};

引用:User Pool Lambda Trigger Sources

关于node.js - 亚马逊认知 : How to implement Post Confirmation Trigger which works only after SignupConfirmation not after ForgotPassword/ResetPassword Confirmation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66223072/

相关文章:

angular - 为什么带有变音符号的关键帧内容会导致额外的空白?

javascript - express JS - 无法从其他外部服务器接收 GET 响应到我的 View

mysql - 如何使用 node.js MySQL 从 MySql DB 获取结果并将它们发送回 API.ai - DialogFlow

javascript - 按数组键值对 JSON 进行排序

css - Mat-Stepper - 将特定步骤更改为点

javascript - Angular 2 - 预加载背景图片?

javascript - NodeJS 和 Express、ENOENT 查看文件

visual-studio - 立即部署多个 aws netcore lambda 函数

aws-lambda - lambda 函数内的跨堆栈引用

c# - 在无服务器上下文中,C# async/await 有什么好处?