Created
August 21, 2020 11:20
-
-
Save sathyarajshetigar/84cee151d48734f6b623a1f03ec1cdbf to your computer and use it in GitHub Desktop.
CodeShare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //#define USE_APPLESIGNIN | |
| #define USE_LUPIDAN_APPLE_SIGNIN | |
| using System.Collections; | |
| using UnityEngine; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Runtime.InteropServices; | |
| using AOT; | |
| using UnityEngine.Events; | |
| #if USE_APPLESIGNIN | |
| using UnityEngine.SignInWithApple; | |
| #endif | |
| using UnityEngine.iOS; | |
| #if USE_LUPIDAN_APPLE_SIGNIN | |
| using AppleAuth; | |
| using AppleAuth.Enums; | |
| using AppleAuth.Extensions; | |
| using AppleAuth.Interfaces; | |
| using AppleAuth.Native; | |
| #endif | |
| namespace IjsUiFramework | |
| { | |
| #if USE_APPLESIGNIN | |
| [RequireComponent(typeof(SignInWithApple))] | |
| #endif | |
| public class SignInWithAppleHelper : MonoBehaviour | |
| { | |
| #if USE_APPLESIGNIN | |
| private SignInWithApple _signInWithApple; | |
| public delegate void SignInWithAppleCallback(bool success, UserInfo userInfo); | |
| private SignInWithAppleCallback _signInWithAppleCallback; | |
| private void Awake() | |
| { | |
| _signInWithApple = GetComponent<SignInWithApple>(); | |
| } | |
| /// <summary> | |
| /// Used for testing | |
| /// </summary> | |
| public void LoginTest() | |
| { | |
| Version currentVersion = new Version(Device.systemVersion); // Parse the version of the current OS | |
| Version ios13 = new Version("13.0"); // Parse the iOS 13.0 version constant | |
| if (currentVersion >= ios13) | |
| { | |
| // Enable the button... | |
| } | |
| Login((success, userInfo) => | |
| { | |
| Game.Log($"LoginTest success {success}"); | |
| if (success) | |
| { | |
| Game.Log($"LoginTest displayName {userInfo.displayName} email {userInfo.email} idToken {userInfo.idToken} userDetectionStatus {userInfo.userDetectionStatus} userId {userInfo.userId}"); | |
| } | |
| }); | |
| } | |
| public void Login(SignInWithAppleCallback callback) | |
| { | |
| _signInWithAppleCallback = callback; | |
| _signInWithApple.Login(OnLogin); | |
| } | |
| private void OnLogin(SignInWithApple.CallbackArgs args) | |
| { | |
| Debug.Log("Sign in with Apple login has completed."); | |
| if (args.error != null) | |
| { | |
| Debug.Log("Errors occurred: " + args.error); | |
| _signInWithAppleCallback?.Invoke(false, default); | |
| } | |
| else | |
| { | |
| GetCredentialState(args.userInfo.userId); | |
| Game.Log($"Display Name: {args.userInfo.displayName ?? ""}\nEmail: {args.userInfo.email ?? ""}\nUser ID: {args.userInfo.userId ?? ""}\nID Token: {args.userInfo.idToken ?? ""}"); | |
| } | |
| } | |
| private void GetCredentialState(string userID) | |
| { | |
| // User id that was obtained from the user signed into your app for the first time. | |
| _signInWithApple.GetCredentialState(userID, OnCredentialState); | |
| } | |
| private void OnCredentialState(SignInWithApple.CallbackArgs args) | |
| { | |
| Game.Log($"User credential state is: {args.credentialState}"); | |
| if (args.error != null) | |
| { | |
| Game.Log($"OnCredentialState Errors occurred: {args.error}"); | |
| _signInWithAppleCallback?.Invoke(false, default); | |
| } | |
| else | |
| { | |
| if (args.credentialState == UserCredentialState.Authorized) | |
| _signInWithAppleCallback?.Invoke(true, args.userInfo); | |
| else | |
| _signInWithAppleCallback?.Invoke(false, default); | |
| } | |
| } | |
| #endif | |
| #if USE_LUPIDAN_APPLE_SIGNIN | |
| public delegate void SignInWithAppleCallback(bool success, string identityToken); | |
| private SignInWithAppleCallback _signInWithAppleCallback; | |
| private const string AppleUserIdKey = "AppleUserId"; | |
| private IAppleAuthManager _appleAuthManager; | |
| private void Start() | |
| { | |
| // If the current platform is supported | |
| if (AppleAuthManager.IsCurrentPlatformSupported) | |
| { | |
| // Creates a default JSON deserializer, to transform JSON Native responses to C# instances | |
| var deserializer = new PayloadDeserializer(); | |
| // Creates an Apple Authentication manager with the deserializer | |
| this._appleAuthManager = new AppleAuthManager(deserializer); | |
| } | |
| // InitializeLoginMenu(); | |
| } | |
| private void Update() | |
| { | |
| // Updates the AppleAuthManager instance to execute | |
| // pending callbacks inside Unity's execution loop | |
| if (this._appleAuthManager != null) | |
| { | |
| this._appleAuthManager.Update(); | |
| } | |
| } | |
| private void InitializeLoginMenu() | |
| { | |
| // Check if the current platform supports Sign In With Apple | |
| if (this._appleAuthManager == null) | |
| { | |
| //this.SetupLoginMenuForUnsupportedPlatform(); | |
| return; | |
| } | |
| // If at any point we receive a credentials revoked notification, we delete the stored User ID, and go back to login | |
| this._appleAuthManager.SetCredentialsRevokedCallback(result => | |
| { | |
| Debug.Log("Received revoked callback " + result); | |
| // this.SetupLoginMenuForSignInWithApple(); | |
| PlayerPrefs.DeleteKey(AppleUserIdKey); | |
| }); | |
| // If we have an Apple User Id available, get the credential status for it | |
| if (PlayerPrefs.HasKey(AppleUserIdKey)) | |
| { | |
| var storedAppleUserId = PlayerPrefs.GetString(AppleUserIdKey); | |
| // this.SetupLoginMenuForCheckingCredentials(); | |
| this.CheckCredentialStatusForUserId(storedAppleUserId); | |
| } | |
| // If we do not have an stored Apple User Id, attempt a quick login | |
| else | |
| { | |
| // this.SetupLoginMenuForQuickLoginAttempt(); | |
| this.AttemptQuickLogin(); | |
| } | |
| } | |
| public void Login(SignInWithAppleCallback callback) | |
| { | |
| _signInWithAppleCallback = callback; | |
| if (PlayerPrefs.HasKey(AppleUserIdKey)) | |
| { | |
| AttemptQuickLogin(); | |
| } | |
| // If we do not have an stored Apple User Id, attempt a quick login | |
| else | |
| { | |
| SignInWithApple(); | |
| } | |
| } | |
| private void CheckCredentialStatusForUserId(string appleUserId) | |
| { | |
| // If there is an apple ID available, we should check the credential state | |
| this._appleAuthManager.GetCredentialState( | |
| appleUserId, | |
| state => | |
| { | |
| switch (state) | |
| { | |
| // If it's authorized, login with that user id | |
| case CredentialState.Authorized: | |
| // this.SetupGameMenu(appleUserId, null); | |
| return; | |
| // If it was revoked, or not found, we need a new sign in with apple attempt | |
| // Discard previous apple user id | |
| case CredentialState.Revoked: | |
| case CredentialState.NotFound: | |
| // this.SetupLoginMenuForSignInWithApple(); | |
| PlayerPrefs.DeleteKey(AppleUserIdKey); | |
| return; | |
| } | |
| }, | |
| error => | |
| { | |
| var authorizationErrorCode = error.GetAuthorizationErrorCode(); | |
| Debug.LogWarning("Error while trying to get credential state " + authorizationErrorCode.ToString() + " " + error.ToString()); | |
| // this.SetupLoginMenuForSignInWithApple(); | |
| }); | |
| } | |
| private void AttemptQuickLogin() | |
| { | |
| var quickLoginArgs = new AppleAuthQuickLoginArgs(); | |
| // Quick login should succeed if the credential was authorized before and not revoked | |
| this._appleAuthManager.QuickLogin( | |
| quickLoginArgs, | |
| credential => | |
| { | |
| // If it's an Apple credential, save the user ID, for later logins | |
| var appleIdCredential = credential as IAppleIDCredential; | |
| if (appleIdCredential != null) | |
| { | |
| PlayerPrefs.SetString(AppleUserIdKey, credential.User); | |
| } | |
| string identityToken = System.Text.Encoding.UTF8.GetString(appleIdCredential.IdentityToken); | |
| _signInWithAppleCallback?.Invoke(true, identityToken); | |
| // this.SetupGameMenu(credential.User, credential); | |
| }, | |
| error => | |
| { | |
| _signInWithAppleCallback?.Invoke(false, string.Empty); | |
| // 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(); | |
| }); | |
| } | |
| private void SignInWithApple() | |
| { | |
| var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName); | |
| this._appleAuthManager.LoginWithAppleId( | |
| loginArgs, | |
| credential => | |
| { | |
| // If a sign in with apple succeeds, we should have obtained the credential with the user id, name, and email, save it | |
| PlayerPrefs.SetString(AppleUserIdKey, credential.User); | |
| var appleIdCredential = credential as IAppleIDCredential; | |
| string identityToken = System.Text.Encoding.UTF8.GetString(appleIdCredential.IdentityToken); | |
| _signInWithAppleCallback?.Invoke(true, identityToken); | |
| }, | |
| error => | |
| { | |
| _signInWithAppleCallback?.Invoke(false, string.Empty); | |
| var authorizationErrorCode = error.GetAuthorizationErrorCode(); | |
| Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString()); | |
| }); | |
| } | |
| #endif | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment