Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- using AppleAuth;
- using AppleAuth.Enums;
- using AppleAuth.Extensions;
- using AppleAuth.Interfaces;
- using AppleAuth.Native;
- using PlayFab;
- using PlayFab.ClientModels;
- public class AppleSignIn : MonoBehaviour
- {
- // Player
- private const string AppleUserIdKey = "AppleUserId";
- private const string AppleIdentityTokenKey = "AppleIdentityTokenId";
- // Modules
- private IAppleAuthManager _appleAuthManager;
- private void Start()
- {
- // If the current platform is supported
- // 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);
- // 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);
- 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.CheckCredentialStatusForUserId(storedAppleUserId);
- }
- // If we do not have an stored Apple User Id, attempt a quick login
- else
- {
- this.AttemptQuickLogin();
- }
- }
- public void SignInWithAppleButtonPressed()
- {
- this.AttemptQuickLogin();
- }
- private void Update()
- {
- if (this._appleAuthManager != null)
- {
- // Updates the AppleAuthManager instance to execute
- // pending callbacks inside Unity's execution loop
- this._appleAuthManager.Update();
- }
- //this.LoginMenu.UpdateLoadingMessage(Time.deltaTime);
- }
- 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:
- 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:
- PlayerPrefs.DeleteKey(AppleUserIdKey);
- return;
- }
- },
- error =>
- {
- var authorizationErrorCode = error.GetAuthorizationErrorCode();
- Debug.LogWarning("Error while trying to get credential state " + authorizationErrorCode.ToString() + " " + error.ToString());
- });
- }
- 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);
- LoginWithApple(appleIdCredential.IdentityToken);
- }
- if (appleIdCredential.IdentityToken != null)
- {
- // Set Identity Token
- var identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length);
- PlayerPrefs.SetString(AppleIdentityTokenKey, identityToken);
- LoginWithApple(appleIdCredential.IdentityToken);
- }
- },
- 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());
- });
- }
- public static void LoginWithApple(byte[] identityToken)
- {
- PlayFabClientAPI.LoginWithApple(new LoginWithAppleRequest
- {
- CreateAccount = true,
- IdentityToken = Encoding.UTF8.GetString(identityToken),
- TitleId = PlayFabSettings.TitleId
- }
- , result =>
- {
- Debug.Log("Login With Apple Success!!");
- }
- , error => { Debug.Log(error.GenerateErrorReport()); });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement