node.js - 在 cognito AdminCreateUser 电子邮件中使用动态占位符

标签 node.js amazon-web-services aws-lambda amazon-cognito

我通过 cognito-idp API 调用 adminCreateUser 创建用户,但我遇到的问题是,如果我创建属于开发或生产的用户,我设置了不同的环境,并且单击电子邮件的链接将是一个不同的 URL。

目前我正在发送一封如下所示的电子邮件:

<h3>Hello {username} !</h3>
<p>To finish your subscription please click on <a href="http://localhost:3000/signup?token={####}" >finish my subscription</a></p>

但正如您所看到的,它仅涵盖开发人员选项,我想发送一封包含一些占位符的电子邮件,无论我们是创建开发人员用户还是生产用户,我都可以动态使用这些占位符,例如使用标签 { url} 将是一个很大的帮助。

这就是我想做的:

<h3>Hello {name} !</h3>
<p>Your username is {username}</p>
<p>To finish your subscription to {project} please click on <a href="{url}{####}" >finish my subscription</a></p>

AWS Cognito 接受吗?我读到您可以通过 lambda 执行类似的操作,但不太确定我是否可以创建自己的变量,或者它是否只是 cognito 中的现有变量。

有更多 Cognito 经验的人可以帮我解决这个问题吗?

非常感谢!

最佳答案

您可以使用 AWS-Cognito-Custom-Message-Trigger 来完成此操作。它有点复杂,因为您正在 lambda 函数中处理消息,但这是在响应中能够访问属性和组名称等的唯一方法。

触发器 lambda 的示例代码:

exports.handler = (event, context, callback) => {
    //
    if(event.userPoolId === "theSpecialUserPool") {
        // Identify why was this function invoked
        if(event.triggerSource === "CustomMessage_AdminCreateUser") {
            // Ensure that your message contains event.request.codeParameter event.request.usernameParameter. This is the placeholder for the code and username that will be sent to your user.
            event.response.smsMessage = "Welcome to the service. Your user name is " + event.request.usernameParameter + " Your temporary password is " + event.request.codeParameter;
            event.response.emailSubject = "Welcome to the service";
            event.response.emailMessage = "Welcome to the service. Your user name is " + event.request.usernameParameter + " Your temporary password is " + event.request.codeParameter;
        }
        // Create custom message for other events
    }
    // Customize messages for other user pools

    // Return to Amazon Cognito
    callback(null, event);
};

此处的文档:https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-custom-message.html

关于node.js - 在 cognito AdminCreateUser 电子邮件中使用动态占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60268905/

相关文章:

amazon-s3 - 尝试使用 s3cmd 在 Amazon S3 文件上设置元数据

amazon-web-services - 使用boto3从sqs接收和删除消息

amazon-web-services - 在计费 panic 中锁定公共(public)站点访问

amazon-web-services - Lambda 返回有效负载 botocore.response.StreamingBody 对象打印但随后在变量中为空

amazon-web-services - AWS S3 CloudFormation 只读权限

node.js - 格式错误的数组文字 : "1" error when updating column in postgres sequelize migration script

javascript - NodeJS 更新 API - 内部服务器错误

mongodb - 如何在使用 Mongoose 进行大量多次插入后执行回调?

node.js - 在 Promise.all 中解析时将 Promise 结果流式传输给客户端

node.js - NodeJS 将文件写入 AWS S3 - Promise.All with async/await 不等待