Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
确认后 Lambda 触发器
Amazon Cognito 会在注册用户确认其用户账户后调用此触发器。在您的确认后 Lambda 函数中,您可以发送自定义消息或添加自定义 API 请求。例如,您可以查询外部系统并为用户填充其他属性。Amazon Cognito 仅对在您的用户群体中注册的用户调用此触发器,而不会针对您使用管理员凭证创建的用户账户调用此触发器。
请求包含已确认用户的当前属性。您的用户池在ConfirmSignUp、AdminConfirmSignUp和上调用您的帖子确认功能。ConfirmForgotPassword当用户在托管登录中确认注册或密码重置时,也会运行此触发器。
确认后 Lambda 触发器参数
Amazon Cognito 传递给此 Lambda 函数的请求是以下参数和 Amazon Cognito 添加到所有请求中的常用参数的组合。
- JSON
-
{
"request": {
"userAttributes": {
"string
": "string
",
. . .
},
"clientMetadata": {
"string
": "string
",
. . .
}
},
"response": {}
}
确认后请求参数
确认后响应参数
预计响应中没有其他返回信息。
确认后示例
此示例 Lambda 函数将使用 Amazon SES 向用户发送确认电子邮件。有关更多信息,请参阅 Amazon Simple Email Service 开发人员指南。
- Node.js
-
// Import required AWS SDK clients and commands for Node.js. Note that this requires
// the `@aws-sdk/client-ses` module to be either bundled with this code or included
// as a Lambda layer.
import { SES, SendEmailCommand } from "@aws-sdk/client-ses";
const ses = new SES();
const handler = async (event) => {
if (event.request.userAttributes.email) {
await sendTheEmail(
event.request.userAttributes.email,
`Congratulations ${event.userName}, you have been confirmed.`,
);
}
return event;
};
const sendTheEmail = async (to, body) => {
const 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>",
};
try {
await ses.send(new SendEmailCommand(eParams));
} catch (err) {
console.log(err);
}
};
export { handler };
Amazon Cognito 将事件信息传递给 Lambda 函数。随后,该函数将相同事件对象随同响应中的任何更改返回给 Amazon Cognito。在 Lambda 控制台中,您可以设置一个测试事件,该事件包含与您的 Lambda 触发器相关的数据。以下是此代码示例的一个测试事件:
- JSON
-
{
"request": {
"userAttributes": {
"email": "user@example.com",
"email_verified": true
}
},
"response": {}
}