Dev/Unity

유니티 iOS 로그인 + Firebase 인증

어클 2024. 7. 20. 16:39
반응형

이글은 애플기기에서 유니티 앱으로 애플로그인을 해보는 방법이다. 추가적으로 Firebase 인증까지 해본다.

 

애플 인증은 링크 설명대로 하면 된다.

https://github.com/lupidan/apple-signin-unity/tree/master

 

GitHub - lupidan/apple-signin-unity: Unity plugin to support Sign In With Apple Id

Unity plugin to support Sign In With Apple Id. Contribute to lupidan/apple-signin-unity development by creating an account on GitHub.

github.com

 

 

 

현재 이글 작성시점 버젼 AppleSignInUnity_v1.4.3 패키지를 추가하고 일단 빌드한다.

https://github.com/lupidan/apple-signin-unity/releases/tag/v1.4.3

 

Release Sign in with Apple Unity Plugin v1.4.3 · lupidan/apple-signin-unity

Changed Updates AddSignInWithAppleWithCompatibility to support new public constructor for PBXCapabilityType introduced in Unity 2022.3.10 Increases minimum target for the macOS bundle to 10.13 Ad...

github.com

 

일단 유니티 빌드를 하고 xCode를 오픈해보자. 

 

 인증서, 프로비저닝 

애플로그인은 Sign in with Apple 기능이 추가되어야 한다.

시뮬레이터로 빌드할시 인증서는 필요 없을 것이다. 상단 All 탭 왼쪽에 +Capability 버튼으로 추가 할 수 있다.

기기에 담아서 테스트 중이라면 인증서 생성시 이기능을 꼭 넣기 바란다. 

방법은 검색하면 많이 나온다.(준비중) 

일단 제대로 인증서를 만들면 Sign in with Apple 기능이 자동으로 추가되는 것을 볼 수 있다. 

 

 실행 및 테스트 

애플로그인과 로그아웃 자동로그인테스트

샘플 빌드시 로그아웃 하는법 (포스팅 준비중)

: 설정 - 프로필 - 로그인 및 보안 - Apple로 로그인 - XC com xxxxx xxxxxxx.  : 선택 - Apple ID 사용 중단

 

● 파이어베이스 인증 (Firebase Authentication) 설치 

이제 로그인과 동시에 Firebase 인증까지 해보자.

유니티 프로젝트 설정에서 Bundle Identifier 이름을 잘 기억하자. 

Firebase console 에서 프로젝트를 생성하고 유니티앱 추가하기를 누른다.

 

 

Apple 앱으로 등록에 체크하고 Bundle Identifier 를 입력한다. 

 

순서대로 GoogleService-Info 추가와 

FirebaseAuth.unitypackage 설치를 끝내자. 다른 패키지는 (설치할 필요없다)

 

인증을 위한 설정을 할 차례다. 좌측 메뉴에서 Authentication을 누르고 시작하기를 누른다. 

 

인증화면에서 로그인 방법탭 클릭 - Apple을 클릭하여 추가한다.  

사용설정 On - 저장. 

 

 

Firebase 인증 코드 추가하기 

https://github.com/lupidan/apple-signin-unity/blob/master/docs/Firebase_NOTES.md

간단히 요약하자면 먼저 FirebaseAuth 가 초기화 된후에

버튼클릭시 Firebase 로그인 함수를 연결해주면된다.

private const string AppleUserIdKey = "AppleUserId"; // 이부분은 그냥 둬도 된다.

  

 

FirebaseAuth 초기화

auth = Firebase.Auth.FirebaseAuth.DefaultInstance;

 

 

로그인 버튼 클릭시 새로 추가한 함수로 연결되도록 수정한다. 

    public void SignInWithAppleButtonPressed()
    {
        this.SetupLoginMenuForAppleSignIn();
        //this.SignInWithApple();  // 애플만 로그인
        this.PerformLoginWithAppleIdAndFirebase(FirebaseAuthCallback); // 파이어 베이스 로그인
    }

    private void FirebaseAuthCallback(FirebaseUser user)
    {
        Debug.Log("user: " + user);
    }

 

 

자동로그인 부분도 새로 추가한 함수로 수정한다.

    private void InitializeLoginMenu()
    {
         .  .   .
   
        // If we do not have an stored Apple User Id, attempt a quick login
        else
        {
            this.SetupLoginMenuForQuickLoginAttempt();
            //this.AttemptQuickLogin(); // 애플 자동록인
            this.PerformQuickLoginWithFirebase(FirebaseAuthCallback); // 파이어베이스 자동 로그인 
        }
    }

 

 

※깃허브 소스로 자동로그인이 안된다면 아래 소스를 참고하세요.

    public void PerformQuickLoginWithFirebase(Action<FirebaseUser> firebaseAuthCallback)
    {
        var quickLoginArgs = new AppleAuthQuickLoginArgs();

        this._appleAuthManager.QuickLogin(
            quickLoginArgs,
            credential =>
            {
                var appleIdCredential = credential as IAppleIDCredential;
                if (appleIdCredential != null)
                {
                    PlayerPrefs.SetString(AppleUserIdKey, credential.User);
                }
                var rawNonce = GenerateRandomString(32);
                this.PerformFirebaseAuthentication(appleIdCredential, rawNonce, firebaseAuthCallback);
                this.SetupGameMenu(credential.User, credential);
            },
            error =>
            {
                // Something went wrong
                // If Quick Login fails, we should show the normal sign in with apple menu, to allow for a normal Sign In with apple
                var authorizationErrorCode = error.GetAuthorizationErrorCode();
                Debug.LogWarning("Quick Login Failed " + authorizationErrorCode.ToString() + " " + error.ToString());
                this.SetupLoginMenuForSignInWithApple();
            });
    }

 

빌드 후 확인 해보자.

끝.

반응형