Advertisement
Guest User

Untitled

a guest
Apr 15th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.81 KB | Gaming | 0 0
  1.     using System.Collections;
  2.     using System.Collections.Generic;
  3.     using System.Text;
  4.     using UnityEngine;
  5.     using AppleAuth;
  6.     using AppleAuth.Enums;
  7.     using AppleAuth.Extensions;
  8.     using AppleAuth.Interfaces;
  9.     using AppleAuth.Native;
  10.     using PlayFab;
  11.     using PlayFab.ClientModels;
  12.    
  13.     public class AppleSignIn : MonoBehaviour
  14.     {
  15.         // Player
  16.         private const string AppleUserIdKey = "AppleUserId";
  17.         private const string AppleIdentityTokenKey = "AppleIdentityTokenId";
  18.         // Modules
  19.         private IAppleAuthManager _appleAuthManager;
  20.    
  21.         private void Start()
  22.         {
  23.             // If the current platform is supported
  24.             // Creates a default JSON deserializer, to transform JSON Native responses to C# instances
  25.             var deserializer = new PayloadDeserializer();
  26.             // Creates an Apple Authentication manager with the deserializer
  27.             this._appleAuthManager = new AppleAuthManager(deserializer);
  28.    
  29.             // If at any point we receive a credentials revoked notification, we delete the stored User ID, and go back to login
  30.             this._appleAuthManager.SetCredentialsRevokedCallback(result =>
  31.             {
  32.                 Debug.Log("Received revoked callback " + result);
  33.                 PlayerPrefs.DeleteKey(AppleUserIdKey);
  34.             });
  35.    
  36.             // If we have an Apple User Id available, get the credential status for it
  37.             if (PlayerPrefs.HasKey(AppleUserIdKey))
  38.             {
  39.                 var storedAppleUserId = PlayerPrefs.GetString(AppleUserIdKey);
  40.                 this.CheckCredentialStatusForUserId(storedAppleUserId);
  41.             }
  42.             // If we do not have an stored Apple User Id, attempt a quick login
  43.             else
  44.             {
  45.                 this.AttemptQuickLogin();
  46.             }
  47.    
  48.         }
  49.    
  50.         public void SignInWithAppleButtonPressed()
  51.         {
  52.             this.AttemptQuickLogin();
  53.         }
  54.    
  55.         private void Update()
  56.         {
  57.             if (this._appleAuthManager != null)
  58.             {
  59.                 // Updates the AppleAuthManager instance to execute
  60.                 // pending callbacks inside Unity's execution loop
  61.                 this._appleAuthManager.Update();
  62.             }
  63.    
  64.             //this.LoginMenu.UpdateLoadingMessage(Time.deltaTime);
  65.         }
  66.    
  67.         private void CheckCredentialStatusForUserId(string appleUserId)
  68.         {
  69.             // If there is an apple ID available, we should check the credential state
  70.             this._appleAuthManager.GetCredentialState(
  71.                 appleUserId,
  72.                 state =>
  73.                 {
  74.                     switch (state)
  75.                     {
  76.                         // If it's authorized, login with that user id
  77.                         case CredentialState.Authorized:
  78.                             return;
  79.    
  80.                         // If it was revoked, or not found, we need a new sign in with apple attempt
  81.                         // Discard previous apple user id
  82.                         case CredentialState.Revoked:
  83.                         case CredentialState.NotFound:
  84.                             PlayerPrefs.DeleteKey(AppleUserIdKey);
  85.                             return;
  86.                     }
  87.                 },
  88.                 error =>
  89.                 {
  90.                     var authorizationErrorCode = error.GetAuthorizationErrorCode();
  91.                     Debug.LogWarning("Error while trying to get credential state " + authorizationErrorCode.ToString() + " " + error.ToString());
  92.                 });
  93.         }
  94.    
  95.         private void AttemptQuickLogin()
  96.         {
  97.             var quickLoginArgs = new AppleAuthQuickLoginArgs();
  98.    
  99.             // Quick login should succeed if the credential was authorized before and not revoked
  100.             this._appleAuthManager.QuickLogin(
  101.                 quickLoginArgs,
  102.                 credential =>
  103.                 {
  104.                     // If it's an Apple credential, save the user ID, for later logins
  105.                     var appleIdCredential = credential as IAppleIDCredential;
  106.                     if (appleIdCredential != null)
  107.                     {
  108.                         PlayerPrefs.SetString(AppleUserIdKey, credential.User);
  109.                         LoginWithApple(appleIdCredential.IdentityToken);
  110.                     }
  111.                     if (appleIdCredential.IdentityToken != null)
  112.                     {
  113.                         // Set Identity Token
  114.                         var identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length);
  115.                         PlayerPrefs.SetString(AppleIdentityTokenKey, identityToken);
  116.                         LoginWithApple(appleIdCredential.IdentityToken);
  117.                     }
  118.                 },
  119.                 error =>
  120.                 {
  121.                     // If Quick Login fails, we should show the normal sign in with apple menu, to allow for a normal Sign In with apple
  122.                     var authorizationErrorCode = error.GetAuthorizationErrorCode();
  123.                     Debug.LogWarning("Quick Login Failed " + authorizationErrorCode.ToString() + " " + error.ToString());
  124.                 });
  125.         }
  126.    
  127.         public static void LoginWithApple(byte[] identityToken)
  128.         {
  129.             PlayFabClientAPI.LoginWithApple(new LoginWithAppleRequest
  130.             {
  131.                 CreateAccount = true,
  132.                 IdentityToken = Encoding.UTF8.GetString(identityToken),
  133.                 TitleId = PlayFabSettings.TitleId
  134.             }
  135.             , result =>
  136.             {
  137.                 Debug.Log("Login With Apple Success!!");
  138.             }
  139.             , error => { Debug.Log(error.GenerateErrorReport()); });
  140.         }
  141.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement