|
@@ -1,19 +1,14 @@
|
|
|
-using System;
|
|
|
|
|
-using System.Collections;
|
|
|
|
|
-using System.Text;
|
|
|
|
|
-using AppleAuth;
|
|
|
|
|
|
|
+using AppleAuth;
|
|
|
using AppleAuth.Enums;
|
|
using AppleAuth.Enums;
|
|
|
using AppleAuth.Extensions;
|
|
using AppleAuth.Extensions;
|
|
|
using AppleAuth.Interfaces;
|
|
using AppleAuth.Interfaces;
|
|
|
using AppleAuth.Native;
|
|
using AppleAuth.Native;
|
|
|
-using Newtonsoft.Json;
|
|
|
|
|
using UnityEngine;
|
|
using UnityEngine;
|
|
|
-using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
public class MainMenu : MonoBehaviour
|
|
public class MainMenu : MonoBehaviour
|
|
|
{
|
|
{
|
|
|
private const string AppleUserIdKey = "AppleUserId";
|
|
private const string AppleUserIdKey = "AppleUserId";
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private IAppleAuthManager _appleAuthManager;
|
|
private IAppleAuthManager _appleAuthManager;
|
|
|
|
|
|
|
|
public LoginMenuHandler LoginMenu;
|
|
public LoginMenuHandler LoginMenu;
|
|
@@ -27,11 +22,10 @@ public class MainMenu : MonoBehaviour
|
|
|
// Creates a default JSON deserializer, to transform JSON Native responses to C# instances
|
|
// Creates a default JSON deserializer, to transform JSON Native responses to C# instances
|
|
|
var deserializer = new PayloadDeserializer();
|
|
var deserializer = new PayloadDeserializer();
|
|
|
// Creates an Apple Authentication manager with the deserializer
|
|
// Creates an Apple Authentication manager with the deserializer
|
|
|
- this._appleAuthManager = new AppleAuthManager(deserializer);
|
|
|
|
|
|
|
+ this._appleAuthManager = new AppleAuthManager(deserializer);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
this.InitializeLoginMenu();
|
|
this.InitializeLoginMenu();
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void Update()
|
|
private void Update()
|
|
@@ -42,7 +36,7 @@ public class MainMenu : MonoBehaviour
|
|
|
{
|
|
{
|
|
|
this._appleAuthManager.Update();
|
|
this._appleAuthManager.Update();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
this.LoginMenu.UpdateLoadingMessage(Time.deltaTime);
|
|
this.LoginMenu.UpdateLoadingMessage(Time.deltaTime);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -51,27 +45,19 @@ public class MainMenu : MonoBehaviour
|
|
|
this.SetupLoginMenuForAppleSignIn();
|
|
this.SetupLoginMenuForAppleSignIn();
|
|
|
this.SignInWithApple();
|
|
this.SignInWithApple();
|
|
|
}
|
|
}
|
|
|
- public void SignInWithAppleButtonPressed2()
|
|
|
|
|
- {
|
|
|
|
|
- this.SignInWithApple();
|
|
|
|
|
- }
|
|
|
|
|
- public void SignInWithAppleButtonPressedDelete()
|
|
|
|
|
- {
|
|
|
|
|
- this.DeleteSignInWithApple();
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
|
|
+
|
|
|
private void InitializeLoginMenu()
|
|
private void InitializeLoginMenu()
|
|
|
{
|
|
{
|
|
|
this.LoginMenu.SetVisible(visible: true);
|
|
this.LoginMenu.SetVisible(visible: true);
|
|
|
this.GameMenu.SetVisible(visible: false);
|
|
this.GameMenu.SetVisible(visible: false);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Check if the current platform supports Sign In With Apple
|
|
// Check if the current platform supports Sign In With Apple
|
|
|
if (this._appleAuthManager == null)
|
|
if (this._appleAuthManager == null)
|
|
|
{
|
|
{
|
|
|
this.SetupLoginMenuForUnsupportedPlatform();
|
|
this.SetupLoginMenuForUnsupportedPlatform();
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// If at any point we receive a credentials revoked notification, we delete the stored User ID, and go back to login
|
|
// 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 =>
|
|
this._appleAuthManager.SetCredentialsRevokedCallback(result =>
|
|
|
{
|
|
{
|
|
@@ -102,7 +88,7 @@ public class MainMenu : MonoBehaviour
|
|
|
this.LoginMenu.SetSignInWithAppleButton(visible: false, enabled: false);
|
|
this.LoginMenu.SetSignInWithAppleButton(visible: false, enabled: false);
|
|
|
this.LoginMenu.SetLoadingMessage(visible: true, message: "Unsupported platform");
|
|
this.LoginMenu.SetLoadingMessage(visible: true, message: "Unsupported platform");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private void SetupLoginMenuForSignInWithApple()
|
|
private void SetupLoginMenuForSignInWithApple()
|
|
|
{
|
|
{
|
|
|
this.LoginMenu.SetVisible(visible: true);
|
|
this.LoginMenu.SetVisible(visible: true);
|
|
@@ -110,7 +96,7 @@ public class MainMenu : MonoBehaviour
|
|
|
this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: true);
|
|
this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: true);
|
|
|
this.LoginMenu.SetLoadingMessage(visible: false, message: string.Empty);
|
|
this.LoginMenu.SetLoadingMessage(visible: false, message: string.Empty);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private void SetupLoginMenuForCheckingCredentials()
|
|
private void SetupLoginMenuForCheckingCredentials()
|
|
|
{
|
|
{
|
|
|
this.LoginMenu.SetVisible(visible: true);
|
|
this.LoginMenu.SetVisible(visible: true);
|
|
@@ -118,7 +104,7 @@ public class MainMenu : MonoBehaviour
|
|
|
this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
|
|
this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
|
|
|
this.LoginMenu.SetLoadingMessage(visible: true, message: "Checking Apple Credentials");
|
|
this.LoginMenu.SetLoadingMessage(visible: true, message: "Checking Apple Credentials");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private void SetupLoginMenuForQuickLoginAttempt()
|
|
private void SetupLoginMenuForQuickLoginAttempt()
|
|
|
{
|
|
{
|
|
|
this.LoginMenu.SetVisible(visible: true);
|
|
this.LoginMenu.SetVisible(visible: true);
|
|
@@ -126,7 +112,7 @@ public class MainMenu : MonoBehaviour
|
|
|
this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
|
|
this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
|
|
|
this.LoginMenu.SetLoadingMessage(visible: true, message: "Attempting Quick Login");
|
|
this.LoginMenu.SetLoadingMessage(visible: true, message: "Attempting Quick Login");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private void SetupLoginMenuForAppleSignIn()
|
|
private void SetupLoginMenuForAppleSignIn()
|
|
|
{
|
|
{
|
|
|
this.LoginMenu.SetVisible(visible: true);
|
|
this.LoginMenu.SetVisible(visible: true);
|
|
@@ -134,7 +120,7 @@ public class MainMenu : MonoBehaviour
|
|
|
this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
|
|
this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
|
|
|
this.LoginMenu.SetLoadingMessage(visible: true, message: "Signing In with Apple");
|
|
this.LoginMenu.SetLoadingMessage(visible: true, message: "Signing In with Apple");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private void SetupGameMenu(string appleUserId, ICredential credential)
|
|
private void SetupGameMenu(string appleUserId, ICredential credential)
|
|
|
{
|
|
{
|
|
|
this.LoginMenu.SetVisible(visible: false);
|
|
this.LoginMenu.SetVisible(visible: false);
|
|
@@ -155,7 +141,7 @@ public class MainMenu : MonoBehaviour
|
|
|
case CredentialState.Authorized:
|
|
case CredentialState.Authorized:
|
|
|
this.SetupGameMenu(appleUserId, null);
|
|
this.SetupGameMenu(appleUserId, null);
|
|
|
return;
|
|
return;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// If it was revoked, or not found, we need a new sign in with apple attempt
|
|
// If it was revoked, or not found, we need a new sign in with apple attempt
|
|
|
// Discard previous apple user id
|
|
// Discard previous apple user id
|
|
|
case CredentialState.Revoked:
|
|
case CredentialState.Revoked:
|
|
@@ -172,11 +158,11 @@ public class MainMenu : MonoBehaviour
|
|
|
this.SetupLoginMenuForSignInWithApple();
|
|
this.SetupLoginMenuForSignInWithApple();
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private void AttemptQuickLogin()
|
|
private void AttemptQuickLogin()
|
|
|
{
|
|
{
|
|
|
var quickLoginArgs = new AppleAuthQuickLoginArgs();
|
|
var quickLoginArgs = new AppleAuthQuickLoginArgs();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Quick login should succeed if the credential was authorized before and not revoked
|
|
// Quick login should succeed if the credential was authorized before and not revoked
|
|
|
this._appleAuthManager.QuickLogin(
|
|
this._appleAuthManager.QuickLogin(
|
|
|
quickLoginArgs,
|
|
quickLoginArgs,
|
|
@@ -186,11 +172,10 @@ public class MainMenu : MonoBehaviour
|
|
|
var appleIdCredential = credential as IAppleIDCredential;
|
|
var appleIdCredential = credential as IAppleIDCredential;
|
|
|
if (appleIdCredential != null)
|
|
if (appleIdCredential != null)
|
|
|
{
|
|
{
|
|
|
- PlayerPrefs.SetString(AppleUserIdKey, credential.User);
|
|
|
|
|
|
|
+ PlayerPrefs.SetString(AppleUserIdKey, credential.User);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
this.SetupGameMenu(credential.User, credential);
|
|
this.SetupGameMenu(credential.User, credential);
|
|
|
- this.onAppleData(credential.User, credential);
|
|
|
|
|
},
|
|
},
|
|
|
error =>
|
|
error =>
|
|
|
{
|
|
{
|
|
@@ -200,11 +185,11 @@ public class MainMenu : MonoBehaviour
|
|
|
this.SetupLoginMenuForSignInWithApple();
|
|
this.SetupLoginMenuForSignInWithApple();
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private void SignInWithApple()
|
|
private void SignInWithApple()
|
|
|
{
|
|
{
|
|
|
var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
|
|
var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
this._appleAuthManager.LoginWithAppleId(
|
|
this._appleAuthManager.LoginWithAppleId(
|
|
|
loginArgs,
|
|
loginArgs,
|
|
|
credential =>
|
|
credential =>
|
|
@@ -212,39 +197,6 @@ public class MainMenu : MonoBehaviour
|
|
|
// If a sign in with apple succeeds, we should have obtained the credential with the user id, name, and email, save it
|
|
// 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);
|
|
PlayerPrefs.SetString(AppleUserIdKey, credential.User);
|
|
|
this.SetupGameMenu(credential.User, credential);
|
|
this.SetupGameMenu(credential.User, credential);
|
|
|
- this.onAppleData(credential.User, credential);
|
|
|
|
|
- },
|
|
|
|
|
- error =>
|
|
|
|
|
- {
|
|
|
|
|
- var authorizationErrorCode = error.GetAuthorizationErrorCode();
|
|
|
|
|
- Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
|
|
|
|
|
- this.SetupLoginMenuForSignInWithApple();
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private void DeleteSignInWithApple() {
|
|
|
|
|
- var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
|
|
|
|
|
-
|
|
|
|
|
- this._appleAuthManager.LoginWithAppleId(
|
|
|
|
|
- loginArgs,
|
|
|
|
|
- credential =>
|
|
|
|
|
- {
|
|
|
|
|
- var appleIdCredential = credential as IAppleIDCredential;
|
|
|
|
|
- if (appleIdCredential.AuthorizationCode != null)
|
|
|
|
|
- {
|
|
|
|
|
- var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode, 0, appleIdCredential.AuthorizationCode.Length);
|
|
|
|
|
- Debug.Log("delete===authorizationCode======= " + authorizationCode.Substring(0, 45));
|
|
|
|
|
- StartCoroutine(DdeleteUserByApple(authorizationCode, (res) =>
|
|
|
|
|
- {
|
|
|
|
|
- Debug.Log($"DdeleteUserByApple service rescode {res.code}, msg {res.msg}");
|
|
|
|
|
- if (res.code == 0)
|
|
|
|
|
- {
|
|
|
|
|
- Debug.Log("删除成功");
|
|
|
|
|
- this.SetupLoginMenuForSignInWithApple();
|
|
|
|
|
- PlayerPrefs.DeleteKey(AppleUserIdKey);
|
|
|
|
|
- }
|
|
|
|
|
- }));
|
|
|
|
|
- }
|
|
|
|
|
},
|
|
},
|
|
|
error =>
|
|
error =>
|
|
|
{
|
|
{
|
|
@@ -252,113 +204,5 @@ public class MainMenu : MonoBehaviour
|
|
|
Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
|
|
Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
|
|
|
this.SetupLoginMenuForSignInWithApple();
|
|
this.SetupLoginMenuForSignInWithApple();
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public void onAppleData(string appleUserId, ICredential receivedCredential)
|
|
|
|
|
- {
|
|
|
|
|
- var appleIdCredential = receivedCredential as IAppleIDCredential;
|
|
|
|
|
- if (appleIdCredential != null)
|
|
|
|
|
- {
|
|
|
|
|
-
|
|
|
|
|
- if (appleIdCredential.IdentityToken != null)
|
|
|
|
|
- {
|
|
|
|
|
- var identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length);
|
|
|
|
|
- Debug.Log("11===identityToken======= " + identityToken);
|
|
|
|
|
- }
|
|
|
|
|
- if (appleIdCredential.AuthorizationCode != null)
|
|
|
|
|
- {
|
|
|
|
|
- var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode, 0, appleIdCredential.AuthorizationCode.Length);
|
|
|
|
|
- Debug.Log("22===authorizationCode======= " + authorizationCode.Substring(0, 45));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- string _email = "";
|
|
|
|
|
- if (appleIdCredential.Email != null)
|
|
|
|
|
- {
|
|
|
|
|
- _email = appleIdCredential.Email;
|
|
|
|
|
- }
|
|
|
|
|
- Debug.Log("33===Email======= " + _email);
|
|
|
|
|
- string _fullName = "";
|
|
|
|
|
- if (appleIdCredential.FullName != null)
|
|
|
|
|
- {
|
|
|
|
|
- var fullName = appleIdCredential.FullName;
|
|
|
|
|
- if (appleIdCredential.FullName.PhoneticRepresentation != null)
|
|
|
|
|
- {
|
|
|
|
|
- var phoneticName = appleIdCredential.FullName.PhoneticRepresentation;
|
|
|
|
|
- //phoneticName.ToLocalizedString()
|
|
|
|
|
- }
|
|
|
|
|
- _fullName = fullName.ToLocalizedString();
|
|
|
|
|
- }
|
|
|
|
|
- Debug.Log("44===FullName======= " + appleIdCredential.FullName);
|
|
|
|
|
- var _identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length);
|
|
|
|
|
- StartCoroutine(LoginByApple(_identityToken, _email, _fullName, (res) =>
|
|
|
|
|
- {
|
|
|
|
|
- Debug.Log($"LoginByApple service rescode {res.code}, msg {res.msg}");
|
|
|
|
|
- if (res.code == 0)
|
|
|
|
|
- {
|
|
|
|
|
- Debug.Log("LoginByApple 登录");
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- this.SetupLoginMenuForSignInWithApple();
|
|
|
|
|
- PlayerPrefs.DeleteKey(AppleUserIdKey);
|
|
|
|
|
- }
|
|
|
|
|
- }));
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
-
|
|
|
|
|
- this.SetupLoginMenuForSignInWithApple();
|
|
|
|
|
- PlayerPrefs.DeleteKey(AppleUserIdKey);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public IEnumerator LoginByApple(string identityToken, string email, string fullName, Action<RequestResult> callback)
|
|
|
|
|
- {
|
|
|
|
|
- string url = "http://192.168.0.112:11432/SmartBowBusinessServer/gameLogin/loginByApple";
|
|
|
|
|
- WWWForm form = new WWWForm();
|
|
|
|
|
- form.AddField("identityToken", identityToken);
|
|
|
|
|
- form.AddField("email", email);
|
|
|
|
|
- form.AddField("fullName", fullName);
|
|
|
|
|
- form.AddField("serverIndex", 0);
|
|
|
|
|
- using (UnityWebRequest request = UnityWebRequest.Post(url, form))
|
|
|
|
|
- {
|
|
|
|
|
- request.timeout = 10;
|
|
|
|
|
- yield return request.SendWebRequest();
|
|
|
|
|
- RequestResult requestResult = new RequestResult();
|
|
|
|
|
- if (request.result == UnityWebRequest.Result.Success)
|
|
|
|
|
- {
|
|
|
|
|
- requestResult = JsonConvert.DeserializeObject<RequestResult>(request.downloadHandler.text);
|
|
|
|
|
- }
|
|
|
|
|
- if (callback != null) callback(requestResult);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public IEnumerator DdeleteUserByApple(string identityToken, Action<RequestResult> callback)
|
|
|
|
|
- {
|
|
|
|
|
- string url = "http://192.168.0.112:11432/SmartBowBusinessServer/gameLogin/deleteUserByApple";
|
|
|
|
|
- WWWForm form = new WWWForm();
|
|
|
|
|
- form.AddField("identityToken", identityToken);
|
|
|
|
|
- using (UnityWebRequest request = UnityWebRequest.Post(url, form))
|
|
|
|
|
- {
|
|
|
|
|
- request.timeout = 10;
|
|
|
|
|
- yield return request.SendWebRequest();
|
|
|
|
|
- RequestResult requestResult = new RequestResult();
|
|
|
|
|
- if (request.result == UnityWebRequest.Result.Success)
|
|
|
|
|
- {
|
|
|
|
|
- requestResult = JsonConvert.DeserializeObject<RequestResult>(request.downloadHandler.text);
|
|
|
|
|
- }
|
|
|
|
|
- if (callback != null) callback(requestResult);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public class RequestResult
|
|
|
|
|
- {
|
|
|
|
|
- public int code = -9999;
|
|
|
|
|
- public object data;
|
|
|
|
|
- public string msg;
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|