Overview
com.111percent.authentications.gameserverless-login
무엇인가요?
Percent GameServerless Login은 게임 서버를 따로 구축하거나 관리하지 않아도 로그인 기능을 구현할 수 있게 해주는 패키지입니다. 내부적으로 Percent-Auth 패키지를 활용합니다.
왜 만들었나요?
게임 개발에서 계정 관리는 필수지만, 특히 서버리스 환경에서 복잡한 인증 시스템을 구현하는 것은 시간과 리소스가 많이 소요됩니다. Percent GameServerless Login은 서버가 없는 게임에서도 안정적인 인증 기능을 구현하기 위해 만들어진 패키지입니다.
어떻게 사용하나요?
준비하기
- Unity 버전:
Unity 2022.3.X
이상 - 지원 OS: Android, iOS
1. 초기화
- ⚠️ 초기화 시점은 Splash 노출 이후에 해주세요. TitleScene을 권장해요.
- 아래 초기화 코드에서는 Guest, Google, Apple 로그인 기능을 활성화 합니다.
using Percent.Authentications.GameServerlessLogin;
private void Awake()
{
// 로그인 서비스 기능을 사용하기 위해 AuthAccountService 정적 클래스를 초기화 합니다.
// 초기화를 진행하기 위해 AuthProviderBuilder 빌더 클래스를 사용합니다.
AuthAccountService.Initialize(
new AuthProviderBuilder()
.UseSignInWithGuest() // Guest 로그인 기능을 사용합니다.
.UseSignInWithGoogle() // Google 로그인 기능을 사용합니다.
.UseSignInWithApple()); // Apple 로그인 기능을 사용합니다.
}
2. 로그인
- 인증서버에 로그인 요청을 시도합니다.
AuthPlatformType
으로 로그인 방식을 지정합니다.- 로그인에 성공하면 로그인 이력이 기기에 저장됩니다.
using Percent.Authentications.PercentAuth.Define;
private async UniTask LogIn()
{
var resultCode = await AuthAccountService.LogInAsync(_providerType);
if (resultCode != EResultCode.Success)
{
Debug.LogError($"Login failed with result code: {resultCode}");
return;
}
Debug.Log("Login successful");
}
3. 자동 로그인
- 최근 로그인 이력으로 로그인을 시도합니다.
- UI를 통한 로그인 프로세스는 생략되며 바로 로그인합니다.
private async UniTask LoginSilentlyAsync()
{
var result = await AuthAccountService.LoginSilentlyAsync();
if (result != EResultCode.Success)
{
// 자동 로그인 실패 시 오류 처리
// 계정 정보가 잘못되거나 없는 경우 자동 로그인에 실패합니다.
Debug.LogWarning($"Silent login failed: {result}");
return;
}
Debug.Log("Silent login successful");
}
4. 로그아웃
- 로그인 이력을 삭제합니다. 로그인 상태인 경우 로그아웃 됩니다.
- ⚠️ 주의: 게스트 계정은 로그아웃 시 계정 정보가 기기에서 완전히 삭제되며, 해당 계정은 유실됩니다.
private async UniTask LogOut()
{
// 로그아웃을 수행합니다.
// 게스트 계정의 경우 계정 정보가 유실되며 복구할 수 없습니다.
await AuthAccountService.LogoutAsync();
Debug.Log("Logout completed");
}
5. 연동된 플랫폼 목록 가져오기
최근 로그인한 계정에 연동된 모든 플랫폼 목록을 반환합니다. 기기에 최근 로그인 이력이 남아있다면, 로그인 이전에도 호출할 수 있습니다. PlatformLinkState를 확인하여 해당 계정이 게스트 계정인지 Idp(Identity Provider)연동 계정인지 구분할 수 있습니다.
private async UniTask FetchLinkedPlatforms()
{
var result = await AuthAccountService.FetchLinkedPlatformList();
if (result.Success is false)
{
Debug.LogError($"Failed to fetch linked platforms: {result.ResultCode}");
return;
}
var platformLinkState = result.LinkState;
var linkedPlatforms = result.LinkedPlatformList.Select(x => x.platformType).ToArray();
Debug.Log($"Platform link state: {platformLinkState}");
Debug.Log($"Linked platforms: {string.Join(", ", linkedPlatforms)}");
}
6. 계정을 특정 플랫폼과 연동하기
private async UniTask LinkPlatform()
{
var result = await AuthAccountService.LinkPlatformAsync(_providerType);
Debug.Log($"Link platform result: {result.ResultCode}");
if (result.HasDuplicatedAccountInfo is false)
{
return;
}
// 이미 다른 계정에 연동된 플랫폼 계정으로 연동을 시도한 경우
Debug.Log($"Duplicated account detected. AccountId: {result.DuplicatedAccountInfo.AccountId}, LinkedAt: {result.DuplicatedAccountInfo.LinkedAt:O}");
}
7. 계정을 특정 플랫폼에서 연동 해지하기
private async UniTask UnlinkPlatform()
{
var resultCode = await AuthAccountService.UnlinkPlatformAsync(_providerType);
if (resultCode != EResultCode.Success)
{
Debug.LogError($"Failed to unlink platform: {resultCode}");
return;
}
Debug.Log("Platform unlinked successfully");
}
8. 계정 이전 (Transfer)
private async UniTask TransferPlatform()
{
var linkResult = await AuthAccountService.LinkPlatformAsync(_providerType);
if (linkResult.HasDuplicatedAccountInfo is false)
{
Debug.Log($"Platform linked successfully: {linkResult.ResultCode}");
return;
}
// 이미 연동한 플랫폼 계정으로 연동에 실패하면,
// 유저에게 플랫폼 계정 이전(Transfer)에 대한 의사를 물어봅니다.
// 계정 이전은 연동 내역을 변경하는 작업으로 반드시 유저의 동의하에 진행해야 합니다.
// TODO: 사용자가 계정이전을 직접 선택할 수 있도록 UI 노출해주세요.
// 다른 계정에 연동되어있는 플랫폼의 연동을 끊고, 접속중인 계정으로 강제 이전(Transfer)합니다.
var transferResult = await AuthAccountService.TransferPlatformAsync(linkResult.PlatformAuthenticateResult);
Debug.Log($"Account transfer result: {transferResult}");
Debug.Log($"Previous account: {linkResult.DuplicatedAccountInfo.AccountId}, LinkedAt: {linkResult.DuplicatedAccountInfo.LinkedAt:O}");
}
9. Error Handling
- 각 작업의 결과는
EResultCode
enum 값을 통해 확인할 수 있습니다. - 개발 또는 QA 시 로그를 확인하여 성공 또는 실패를 확인합니다.
샘플 코드
해당 패키지는 샘플 코드를 제공합니다.
PPM(Percent Package Manager) 또는 UPM(Unity Package Manager)을 통해 샘플 코드를 import 할 수 있습니다.