

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 確認後 Lambda 觸發程序
<a name="user-pool-lambda-post-confirmation"></a>

Amazon Cognito 會在已註冊的使用者確認其使用者帳戶後，調用此觸發程序。在您的確認後 Lambda 函數中，您可以傳送自訂訊息或新增自訂 API 請求。例如，您可以查詢外部系統並對使用者填入其他屬性。Amazon Cognito 只會針對在您的使用者集區中註冊的使用者調用此觸發程序，而不會針對您使用管理員憑證建立的使用者帳戶調用此觸發程序。

請求中包含已確認使用者的目前屬性。您的使用者集區會在 [ConfirmSignUp](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ConfirmSignUp.html)、[AdminConfirmSignUp](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminConfirmSignUp.html) 和 [ConfirmForgotPassword](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ConfirmForgotPassword.html) 上叫用您的文章確認函數。當使用者在[受管登入](cognito-user-pools-managed-login.md)中確認註冊或密碼重設時，此觸發也會執行。

**Topics**
+ [確認後 Lambda 觸發程序參數](#cognito-user-pools-lambda-trigger-syntax-post-confirmation)
+ [確認後範例](#aws-lambda-triggers-post-confirmation-example)

## 確認後 Lambda 觸發程序參數
<a name="cognito-user-pools-lambda-trigger-syntax-post-confirmation"></a>

Amazon Cognito 傳遞至此 Lambda 函數的請求，是以下參數和 Amazon Cognito 新增至所有請求的[常用參數](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-working-with-lambda-triggers.html#cognito-user-pools-lambda-trigger-syntax-shared)之組合。

------
#### [ JSON ]

```
{
    "request": {
            "userAttributes": {
                "string": "string",
                . . .
            },
            "clientMetadata": {
            	"string": "string",
            	. . .
            }
        },
    "response": {}
}
```

------

### 確認後請求參數
<a name="cognito-user-pools-lambda-trigger-syntax-post-confirmation-request"></a>

**userAttributes**  
代表使用者屬性的一或多個鍵值組。

**clientMetadata**  
您可以做為 Lambda 函數的自訂輸入提供的一個或多個鍵值組，該函數是您用於確認後觸發程序所指定。您可以使用下列 API 動作中的 ClientMetadata 參數，將此資料傳遞至您的 Lambda 函數：[AdminConfirmSignUp](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminConfirmSignUp.html)、[ConfirmForgotPassword](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ConfirmForgotPassword.html)、[ConfirmSignUp](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ConfirmSignUp.html) 和 [SignUp](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_SignUp.html)。

### 確認後回應參數
<a name="cognito-user-pools-lambda-trigger-syntax-post-confirmation-response"></a>

回應中不應有額外的傳回資訊。

## 確認後範例
<a name="aws-lambda-triggers-post-confirmation-example"></a>

這個範例 Lambda 函數會使用 Amazon SES，將確認電子郵件訊息傳送給您的使用者。如需詳細資訊，請參閱《[Amazon Simple Storage Service 開發人員指南](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/)》。

------
#### [ 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": {}
}
```

------