|
- using AppleAuth;
- using AppleAuth.Enums;
- using AppleAuth.Extensions;
- using AppleAuth.Interfaces;
- using AppleAuth.Native;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
-
- public class UILogin : UIBase
- {
- private const string AppleUserIdKey = "AppleUserId";
- private IAppleAuthManager _appleAuthManager;
-
-
-
- public override void OnOpen(params object[] args) {
- // 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);
- }
-
- if (PlayerPrefs.HasKey(AppleUserIdKey))
- {
- var storedAppleUserId = PlayerPrefs.GetString(AppleUserIdKey);
- this.CheckCredentialStatusForUserId(storedAppleUserId);
- }
- }
- public override void OnClose() { }
-
- private void Update()
- {
- // Updates the AppleAuthManager instance to execute
- // pending callbacks inside Unity's execution loop
- if (this._appleAuthManager != null)
- {
- this._appleAuthManager.Update();
- }
- }
-
- public void ClickIOSLoginBtn()
- {
- Debug.Log("IOS Login Clicked");
- 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:
- PlayerPrefs.DeleteKey(AppleUserIdKey);
- return;
- }
- },
- error =>
- {
- var authorizationErrorCode = error.GetAuthorizationErrorCode();
- Debug.LogWarning("Error while trying to get credential state " + authorizationErrorCode.ToString() + " " + error.ToString());
- });
- }
-
- 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());
-
- });
- }
-
- private void SetupGameMenu(string appleUserId, ICredential credential)
- {
- //var appleIdCredential = credential as IAppleIDCredential;
- //var passwordCredential = receivedCredential as IPasswordCredential;
- GameData.Instance.appleUserId = appleUserId;
- GameData.Instance.isYouke = false;
- UIManager.Instance.OpenUI<UIMainMenu>();
- }
-
- public void ClickYkLoginBtn()
- {
- Debug.Log("YouKe Login Clicked");
- //GameData.Instance.username = "visitor";
- GameData.Instance.appleUserId = "123";
- GameData.Instance.isYouke = false;
- UIManager.Instance.OpenUI<UIMainMenu>();
- }
- }
|