# カスタム 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. **カスタム ID** を Unity 用の ID プロバイダーとして追加します。

   1. Unity エディターメニューで、**Edit** (編集) > **Project Settings...** (プロジェクト設定...) を選択し、ナビゲーションメニューから **Services** (サービス) > **Authentication** を選択します。
   2. **ID Providers** (ID プロバイダー) を **Custom** (カスタム) に設定し、**Add** (追加) を選択します。

2. [サービスアカウントを作成](https://services.docs.unity.com/docs/service-account-auth/#create-a-service-account)し、プロジェクトロール **[Player Authentication Token Issuer](https://services.docs.unity.com/docs/service-account-auth/#player-authentication-token-issuer)** (プレイヤー認証トークン発行者) を追加します。

## 戻ってきたプレイヤーのサインインまたは新しいプレイヤーの作成##set-up-a-custom-id-sign-in

1. カスタム ID 認証を使用してプレイヤーをサインインさせるには、独自のゲームサーバーにリクエストを送信して、Unity Authentication Service の**アクセストークン** と **セッショントークン** を取得する必要があります。

   ゲームサーバーで、以下を行います。

   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 を呼び出します。

## Authentication トークンの処理

`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);
    }
}
```

サンプルコードはメソッドのみであり、クラスではありません。
