# 커스텀 ID 로그인

> Provide a Custom ID sign-in option to enable players to authenticate using your custom identity system in your game.

###### 최소 SDK 버전: 3.1.0##minimum-sdk-version-310

이 문서에서는 게임에서 커스텀 토큰으로 플레이어 인증을 설정하는 다음 시나리오에 대해 설명합니다.

* 커스텀 ID 제공업체 로그인을 설정합니다.
* 기존 사용자를 로그인시키거나 새 사용자를 생성합니다.

게임 내 플레이어에게 커스텀 ID 로그인 옵션을 제공하려면 앱을 설정하여 커스텀 ID 제공업체를 통한 로그인을 활성화하십시오.

## 커스텀 ID 로그인 설정

1. 다음과 같은 방법으로 Unity의 ID 제공업체로 **커스텀 ID**를 추가합니다.

   1. Unity 에디터 메뉴에서 **Edit** > \*\*Project Settings…\*\*로 이동한 후, 내비게이션 메뉴에서 **Services** > **Authentication**을 선택합니다.
   2. **ID Providers**를 **Custom**으로 설정한 후, **Add**를 선택합니다.

2. [서비스 계정을 생성](https://services.docs.unity.com/docs/service-account-auth/#create-a-service-account)하고 프로젝트 역할 \*\*[플레이어 인증 토큰 발급자](https://services.docs.unity.com/docs/service-account-auth/#player-authentication-token-issuer)\*\*를 추가합니다.

## 기존 플레이어 로그인 또는 신규 플레이어 생성##set-up-a-custom-id-sign-in

1. 커스텀 ID 인증을 사용해 플레이어를 로그인하려면 자체 게임 서버에 Unity Authentication 서비스 **액세스 토큰**과 **세션 토큰**을 요청해야 합니다.

   사용자 게임 서버:

   1. [토큰 교환 API](https://services.docs.unity.com/docs/service-account-auth/#authenticate-an-api-using-a-stateless-token)를 호출하여 상태 비보존 토큰을 검색합니다.
   2. [커스텀 ID API로 로그인](https://services.docs.unity.com/player-auth/v1/#tag/Player-Authentication/operation/SignInWithCustomID)을 호출합니다.

2. 게임 서버에서 가져온 **액세스 토큰**과 **세션 토큰**을 사용해 `ProcessAuthenticationTokens` API를 호출합니다.

## 인증 토큰 처리

`ProcessAuthenticationTokens`를 통해 Unity Authentication 서비스 액세스 토큰을 처리하고, 게임에 연동된 플레이어 인증이 필요한 다른 UGS SDK에서 토큰을 사용할 수 있습니다. 액세스 토큰이 만료되었으므로 액세스 토큰을 수동으로 새로 고친 후 새 액세스 토큰으로 `ProcessAuthenticationTokens`를 호출해야 합니다. 액세스 토큰과 세션 토큰을 모두 사용하여 `ProcessAuthenticationTokens`를 호출할 수 있습니다. Unity Authentication SDK는 액세스 토큰이 만료되기 전에 액세스 토큰을 새로 고치고, 플레이어의 세션을 활성 상태로 유지하고, 캐시된 플레이어로 로그인을 활성화합니다.

```cs
using Unity.Services.Authentication;

void SignUserWithCustomTokenWithAutoRefresh()
{
    try
    {
        // Check if a cached player already exists by checking if the session token exists
        if (AuthenticationService.Instance.SessionTokenExists) 
        {
            // This call will sign in the cached player.
            await AuthenticationService.Instance.SignInAnonymouslyAsync();
            Debug.Log("Cached user sign in succeeded!");
        }
        else
        {
            var userTokens = // Fetch the user tokens using your method calls
            AuthenticationService.Instance.ProcessAuthenticationTokens(userTokens.AccessToken, userTokens.SessionToken)
        }
        // Shows how to get the playerID
        Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");   
    }
    catch (AuthenticationException ex)
    {
        // Compare error code to AuthenticationErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
        // Compare error code to CommonErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    } 
    catch (Exception ex) {
        // Handle exceptions from your method call
        Debug.LogException(ex);
    }
}
```

샘플 코드는 클래스가 아니라 메서드뿐임을 유의해 주십시오.
