|
- 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);
- }
- }
- 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 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)
- {
- //this.LoginMenu.SetVisible(visible: false);
- //this.GameMenu.SetVisible(visible: true);
- this.SetupAppleData(appleUserId, credential);
- }
-
- public void SetupAppleData(string appleUserId, ICredential receivedCredential)
- {
- var appleIdCredential = receivedCredential as IAppleIDCredential;
- var passwordCredential = receivedCredential as IPasswordCredential;
- if (appleIdCredential != null)
- {
- var stringBuilder = new StringBuilder();
- stringBuilder.AppendLine("RECEIVED APPLE ID CREDENTIAL.\nYOU CAN LOGIN/CREATE A USER WITH THIS");
- stringBuilder.AppendLine("<b>Username:</b> " + appleIdCredential.User);
- stringBuilder.AppendLine("<b>Real user status:</b> " + appleIdCredential.RealUserStatus.ToString());
-
- if (appleIdCredential.State != null)
- stringBuilder.AppendLine("<b>State:</b> " + appleIdCredential.State);
-
- if (appleIdCredential.IdentityToken != null)
- {
- var identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length);
- stringBuilder.AppendLine("<b>Identity token (" + appleIdCredential.IdentityToken.Length + " bytes)</b>");
- stringBuilder.AppendLine(identityToken.Substring(0, 45) + "...");
- }
-
- if (appleIdCredential.AuthorizationCode != null)
- {
- var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode, 0, appleIdCredential.AuthorizationCode.Length);
- stringBuilder.AppendLine("<b>Authorization Code (" + appleIdCredential.AuthorizationCode.Length + " bytes)</b>");
- stringBuilder.AppendLine(authorizationCode.Substring(0, 45) + "...");
- }
-
- if (appleIdCredential.AuthorizedScopes != null)
- stringBuilder.AppendLine("<b>Authorized Scopes:</b> " + string.Join(", ", appleIdCredential.AuthorizedScopes));
-
- if (appleIdCredential.Email != null)
- {
- stringBuilder.AppendLine();
- stringBuilder.AppendLine("<b>EMAIL RECEIVED: YOU WILL ONLY SEE THIS ONCE PER SIGN UP. SEND THIS INFORMATION TO YOUR BACKEND!</b>");
- stringBuilder.AppendLine("<b>You can test this again by revoking credentials in Settings</b>");
- stringBuilder.AppendLine("<b>Email:</b> " + appleIdCredential.Email);
- }
-
- if (appleIdCredential.FullName != null)
- {
- var fullName = appleIdCredential.FullName;
- stringBuilder.AppendLine();
- stringBuilder.AppendLine("<b>NAME RECEIVED: YOU WILL ONLY SEE THIS ONCE PER SIGN UP. SEND THIS INFORMATION TO YOUR BACKEND!</b>");
- stringBuilder.AppendLine("<b>You can test this again by revoking credentials in Settings</b>");
- stringBuilder.AppendLine("<b>Name:</b> " + fullName.ToLocalizedString());
- stringBuilder.AppendLine("<b>Name (Short):</b> " + fullName.ToLocalizedString(PersonNameFormatterStyle.Short));
- stringBuilder.AppendLine("<b>Name (Medium):</b> " + fullName.ToLocalizedString(PersonNameFormatterStyle.Medium));
- stringBuilder.AppendLine("<b>Name (Long):</b> " + fullName.ToLocalizedString(PersonNameFormatterStyle.Long));
- stringBuilder.AppendLine("<b>Name (Abbreviated):</b> " + fullName.ToLocalizedString(PersonNameFormatterStyle.Abbreviated));
-
- if (appleIdCredential.FullName.PhoneticRepresentation != null)
- {
- var phoneticName = appleIdCredential.FullName.PhoneticRepresentation;
- stringBuilder.AppendLine("<b>Phonetic name:</b> " + phoneticName.ToLocalizedString());
- stringBuilder.AppendLine("<b>Phonetic name (Short):</b> " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Short));
- stringBuilder.AppendLine("<b>Phonetic name (Medium):</b> " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Medium));
- stringBuilder.AppendLine("<b>Phonetic name (Long):</b> " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Long));
- stringBuilder.AppendLine("<b>Phonetic name (Abbreviated):</b> " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Abbreviated));
- }
- }
- }
- }
-
- public void ClickYkLoginBtn()
- {
- Debug.Log("YouKe Login Clicked");
- }
- }
|