我通过 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/