123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using AppleAuth;
- using AppleAuth.Enums;
- using AppleAuth.Extensions;
- using AppleAuth.Interfaces;
- using AppleAuth.Native;
- using UnityEngine;
-
- public class MainMenu : MonoBehaviour
- {
- private const string AppleUserIdKey = "AppleUserId";
-
- private IAppleAuthManager _appleAuthManager;
-
- public LoginMenuHandler LoginMenu;
- public GameMenuHandler GameMenu;
-
- 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);
- }
-
- this.InitializeLoginMenu();
- }
-
- private void Update()
- {
- // Updates the AppleAuthManager instance to execute
- // pending callbacks inside Unity's execution loop
- if (this._appleAuthManager != null)
- {
- this._appleAuthManager.Update();
- }
-
- this.LoginMenu.UpdateLoadingMessage(Time.deltaTime);
- }
-
- public void SignInWithAppleButtonPressed()
- {
- this.SetupLoginMenuForAppleSignIn();
- this.SignInWithApple();
- }
-
- private void InitializeLoginMenu()
- {
- this.LoginMenu.SetVisible(visible: true);
- this.GameMenu.SetVisible(visible: false);
-
- // 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();
- }
- }
-
- private void SetupLoginMenuForUnsupportedPlatform()
- {
- this.LoginMenu.SetVisible(visible: true);
- this.GameMenu.SetVisible(visible: false);
- this.LoginMenu.SetSignInWithAppleButton(visible: false, enabled: false);
- this.LoginMenu.SetLoadingMessage(visible: true, message: "Unsupported platform");
- }
-
- private void SetupLoginMenuForSignInWithApple()
- {
- this.LoginMenu.SetVisible(visible: true);
- this.GameMenu.SetVisible(visible: false);
- this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: true);
- this.LoginMenu.SetLoadingMessage(visible: false, message: string.Empty);
- }
-
- private void SetupLoginMenuForCheckingCredentials()
- {
- this.LoginMenu.SetVisible(visible: true);
- this.GameMenu.SetVisible(visible: false);
- this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
- this.LoginMenu.SetLoadingMessage(visible: true, message: "Checking Apple Credentials");
- }
-
- private void SetupLoginMenuForQuickLoginAttempt()
- {
- this.LoginMenu.SetVisible(visible: true);
- this.GameMenu.SetVisible(visible: false);
- this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
- this.LoginMenu.SetLoadingMessage(visible: true, message: "Attempting Quick Login");
- }
-
- private void SetupLoginMenuForAppleSignIn()
- {
- this.LoginMenu.SetVisible(visible: true);
- this.GameMenu.SetVisible(visible: false);
- this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
- this.LoginMenu.SetLoadingMessage(visible: true, message: "Signing In with Apple");
- }
-
- private void SetupGameMenu(string appleUserId, ICredential credential)
- {
- this.LoginMenu.SetVisible(visible: false);
- this.GameMenu.SetVisible(visible: true);
- this.GameMenu.SetupAppleData(appleUserId, credential);
- }
-
- 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);
- }
-
- this.SetupGameMenu(credential.User, credential);
- },
- error =>
- {
- // 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);
- this.SetupGameMenu(credential.User, credential);
- },
- error =>
- {
- var authorizationErrorCode = error.GetAuthorizationErrorCode();
- Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
- this.SetupLoginMenuForSignInWithApple();
- });
- }
- }
|