

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# 사후 인증 Lambda 트리거
<a name="user-pool-lambda-post-authentication"></a>

사후 인증 트리거는 사용자의 인증 흐름을 변경하지 않습니다. Amazon Cognito는 인증이 완료된 후 사용자가 토큰을 수신하기 전에 이 Lambda를 호출합니다. 다음 로그인 시 반영될 로깅 또는 사용자 프로필 조정과 같은 인증 이벤트의 사용자 지정 후 처리를 추가하려는 경우 인증 후 트리거를 추가합니다.

요청 본문을 Amazon Cognito에 반환하지 않는 사후 인증 Lambda를 사용하면 여전히 인증이 완료되지 않을 수 있습니다. 자세한 내용은 [Lambda 트리거에 대해 알아야 할 사항](cognito-user-pools-working-with-lambda-triggers.md#important-lambda-considerations) 단원을 참조하십시오.

**Topics**
+ [인증 흐름 개요](#user-pool-lambda-post-authentication-1)
+ [사후 인증 Lambda 트리거 파라미터](#cognito-user-pools-lambda-trigger-syntax-post-auth)
+ [사후 인증 예제](#aws-lambda-triggers-post-authentication-example)

## 인증 흐름 개요
<a name="user-pool-lambda-post-authentication-1"></a>

![\[사후 인증 Lambda 트리거 - 클라이언트 흐름\]](http://docs.aws.amazon.com/ko_kr/cognito/latest/developerguide/images/lambda-post-authentication-1.png)


자세한 내용은 [인증 세션의 예](authentication.md#amazon-cognito-user-pools-authentication-flow) 단원을 참조하십시오.

## 사후 인증 Lambda 트리거 파라미터
<a name="cognito-user-pools-lambda-trigger-syntax-post-auth"></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",
             . . .
         },
         "newDeviceUsed": boolean,
         "clientMetadata": {
             "string": "string",
             . . .
            }
        },
    "response": {}
}
```

------

### 사후 인증 요청 파라미터
<a name="cognito-user-pools-lambda-trigger-syntax-post-auth-request"></a>

**newDeviceUsed**  
이 플래그는 사용자가 새로운 디바이스에 로그인했는지 여부를 표시합니다. 기억된 사용자 풀의 디바이스 값이 `Always` 또는 `User Opt-In`으로 설정된 경우에만 Amazon Cognito에서 이 플래그를 설정합니다.

**userAttributes**  
사용자 속성을 나타내는 하나 이상의 이름-값 페어입니다.

**clientMetadata**  
사후 인증 트리거에 지정하는 Lambda 함수에 사용자 지정 입력으로 제공할 수 있는 하나 이상의 키-값 페어입니다. 이 데이터를 Lambda 함수에 전달하려면 [AdminRespondToAuthChallenge](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminRespondToAuthChallenge.html) 및 [RespondToAuthChallenge](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_RespondToAuthChallenge.html) API 작업에서 ClientMetadata 파라미터를 사용합니다. Amazon Cognito는 사후 승인 함수에 전달하는 요청에 있는 [AdminInitiateAuth](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminInitiateAuth.html) 및 [InitiateAuth](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html) API 작업의 ClientMetadata 파라미터에서 전달된 데이터를 포함하지 않습니다.

### 사후 인증 응답 파라미터
<a name="cognito-user-pools-lambda-trigger-syntax-post-auth-response"></a>

Amazon Cognito는 응답에서 추가 반환 정보를 기대하지 않습니다. 함수는 API 작업을 사용하여 리소스를 쿼리 및 수정하거나 이벤트 메타데이터를 외부 시스템에 기록할 수 있습니다.

## 사후 인증 예제
<a name="aws-lambda-triggers-post-authentication-example"></a>

이 사후 인증 Lambda 함수 샘플은 CloudWatch Logs에 성공적으로 로그인하여 데이터를 전송합니다.

------
#### [ Node.js ]

```
const handler = async (event) => {
  // Send post authentication data to Amazon CloudWatch logs
  console.log("Authentication successful");
  console.log("Trigger function =", event.triggerSource);
  console.log("User pool = ", event.userPoolId);
  console.log("App client ID = ", event.callerContext.clientId);
  console.log("User ID = ", event.userName);

  return event;
};

export { handler };
```

------
#### [ Python ]

```
import os
def lambda_handler(event, context):

    # Send post authentication data to Cloudwatch logs
    print ("Authentication successful")
    print ("Trigger function =", event['triggerSource'])
    print ("User pool = ", event['userPoolId'])
    print ("App client ID = ", event['callerContext']['clientId'])
    print ("User ID = ", event['userName'])

    # Return to Amazon Cognito
    return event
```

------

Amazon Cognito는 이벤트 정보를 Lambda 함수에 전달합니다. 그런 다음 함수는 응답이 변경되면 동일한 이벤트 객체를 Amazon Cognito에 반환합니다. Lambda 콘솔에서 해당 Lambda 트리거와 관련 있는 데이터로 테스트 이벤트를 설정할 수 있습니다. 다음은 이 코드 샘플의 테스트 이벤트입니다.

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

```
{
  "triggerSource": "testTrigger",
  "userPoolId": "testPool",
  "userName": "testName",
  "callerContext": {
      "clientId": "12345"
  },
  "response": {}
}
```

------