# 사용자 이름/비밀번호

> Provide a username and password option to enable players to authenticate using custom credentials in your game.

###### 최소 SDK 버전: 2.7.2##minimum-sdk-version-272

게임에서 사용자 이름/비밀번호를 사용해 플레이어의 인증을 설정하는 다음 시나리오를 참조하십시오.

* 사용자 이름/비밀번호를 설정합니다.
* 기존 사용자를 로그인시키거나 새 사용자를 생성합니다.
* 익명 로그인에서 사용자 이름/비밀번호 계정을 통한 플랫폼 로그인으로 사용자를 업데이트합니다.

참고: 먼저 SDK를 [초기화](../get-started#install-the-sdk)해야 합니다.

## 사용자 이름/비밀번호 설정##set-up-the-username--password-provider

1. Unity Authentication의 ID 제공업체를 사용자 이름/비밀번호로 설정합니다.
   1. Unity 에디터 메뉴에서 **Edit** > **Project Settings...** 로 이동한 후, **Services** > **Authentication**을 선택합니다.
   2. **ID Providers**를 **Username/Password**로 설정한 후, **Add**를 선택합니다.
   3. **Save**를 선택합니다.

## 가입 또는 기존 플레이어 로그인##sign-up-or-sign-in-a-returning-player

`SignUpWithUsernamePasswordAsync` 메서드를 통해 사용자 이름/비밀번호 자격 증명을 사용하여 새 플레이어를 생성합니다.

> **Note:**
>
> **참고 1**: **사용자 이름**은 대소문자를 구분하지 않으며, 3~~20자여야 하고, 문자, 숫자, 기호(예: *.*, *-*, *@* 또는 *\_*)만 지원합니다. **참고 2**: **비밀번호**는 대소문자를 구분하며, 8~~30자여야 하고, 소문자, 대문자, 숫자, 기호가 각각 1개 이상 사용되어야 합니다.

```cs
async Task SignUpWithUsernamePasswordAsync(string username, string password)
{
    try
    {
        await AuthenticationService.Instance.SignUpWithUsernamePasswordAsync(username, password);
        Debug.Log("SignUp is successful.");
    }
    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);
    }
}
```

`SignInWithUsernamePasswordAsync` 메서드를 통해 사용자 이름/비밀번호 자격 증명을 사용하여 기존 플레이어를 로그인시킵니다.

```cs
async Task SignInWithUsernamePasswordAsync(string username, string password)
{
    try
    {
        await AuthenticationService.Instance.SignInWithUsernamePasswordAsync(username, password);
        Debug.Log("SignIn is successful.");
    }
    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);
    }
}
```

## 익명 로그인에서 사용자 이름/비밀번호 로그인으로 플레이어 업데이트##link-an-existing-player-to-username--password

익명 인증을 설정한 후 익명 로그인에서 사용자 이름/비밀번호 계정을 생성하여 로그인하도록 플레이어를 업그레이드하려는 경우, 플레이어에게 자격 증명을 입력하는 창을 표시해야 합니다. `AddUsernamePasswordAsync` API를 호출하여 새 사용자 이름/비밀번호 계정을 생성한 다음 플레이어에게 추가합니다.

SDK에 캐시된 플레이어가 있는 경우, 새 사용자 이름/비밀번호를 생성하여 캐시된 플레이어에게 추가합니다.

1. `SignInAnonymouslyAsync`를 사용해 캐시된 플레이어의 계정에 로그인합니다.
2. 새 계정을 생성하고 `AddUsernamePasswordAsync`를 사용해 생성된 계정을 캐시된 플레이어의 계정에 추가합니다.

> **Note:**
>
> **참고**: 사용자 아이디/비밀번호 계정은 계정에 추가된 후에는 삭제할 수 없습니다.

```cs
async Task AddUsernamePasswordAsync(string username, string password)
{
    try
    {
        await AuthenticationService.Instance.AddUsernamePasswordAsync(username, password);
        Debug.Log("Username and password added.");
    }
    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);
    }
}
```

## 플레이어의 비밀번호 변경##change-a-players-password

플레이어에게 사용자 이름/비밀번호 계정의 비밀번호를 변경할 수 있는 옵션을 제공합니다.

사용자가 로그인되어 있어야 하며 **현재 비밀번호**와 **새 비밀번호**를 입력하도록 사용자에게 요청한 후, `UpdatePasswordAsync`를 호출해야 합니다. `UpdatePasswordAsync`는 사용자의 인증 상태를 유지하되 로그인된 다른 모든 기기에서는 사용자에게 다시 로그인하도록 요청합니다.

> **Note:**
>
> **참고**: `UpdatePasswordAsync` 호출이 성공하면 사용자는 캐시된 모든 기기에서 로그아웃됩니다.

```cs
async Task UpdatePasswordAsync(string currentPassword, string newPassword)
{
    try
    {
        await AuthenticationService.Instance.UpdatePasswordAsync(currentPassword, newPassword);
        Debug.Log("Password updated.");
    }
    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);
    }
}
```
