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("Username: " + appleIdCredential.User);
stringBuilder.AppendLine("Real user status: " + appleIdCredential.RealUserStatus.ToString());
if (appleIdCredential.State != null)
stringBuilder.AppendLine("State: " + appleIdCredential.State);
if (appleIdCredential.IdentityToken != null)
{
var identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length);
stringBuilder.AppendLine("Identity token (" + appleIdCredential.IdentityToken.Length + " bytes)");
stringBuilder.AppendLine(identityToken.Substring(0, 45) + "...");
}
if (appleIdCredential.AuthorizationCode != null)
{
var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode, 0, appleIdCredential.AuthorizationCode.Length);
stringBuilder.AppendLine("Authorization Code (" + appleIdCredential.AuthorizationCode.Length + " bytes)");
stringBuilder.AppendLine(authorizationCode.Substring(0, 45) + "...");
}
if (appleIdCredential.AuthorizedScopes != null)
stringBuilder.AppendLine("Authorized Scopes: " + string.Join(", ", appleIdCredential.AuthorizedScopes));
if (appleIdCredential.Email != null)
{
stringBuilder.AppendLine();
stringBuilder.AppendLine("EMAIL RECEIVED: YOU WILL ONLY SEE THIS ONCE PER SIGN UP. SEND THIS INFORMATION TO YOUR BACKEND!");
stringBuilder.AppendLine("You can test this again by revoking credentials in Settings");
stringBuilder.AppendLine("Email: " + appleIdCredential.Email);
}
if (appleIdCredential.FullName != null)
{
var fullName = appleIdCredential.FullName;
stringBuilder.AppendLine();
stringBuilder.AppendLine("NAME RECEIVED: YOU WILL ONLY SEE THIS ONCE PER SIGN UP. SEND THIS INFORMATION TO YOUR BACKEND!");
stringBuilder.AppendLine("You can test this again by revoking credentials in Settings");
stringBuilder.AppendLine("Name: " + fullName.ToLocalizedString());
stringBuilder.AppendLine("Name (Short): " + fullName.ToLocalizedString(PersonNameFormatterStyle.Short));
stringBuilder.AppendLine("Name (Medium): " + fullName.ToLocalizedString(PersonNameFormatterStyle.Medium));
stringBuilder.AppendLine("Name (Long): " + fullName.ToLocalizedString(PersonNameFormatterStyle.Long));
stringBuilder.AppendLine("Name (Abbreviated): " + fullName.ToLocalizedString(PersonNameFormatterStyle.Abbreviated));
if (appleIdCredential.FullName.PhoneticRepresentation != null)
{
var phoneticName = appleIdCredential.FullName.PhoneticRepresentation;
stringBuilder.AppendLine("Phonetic name: " + phoneticName.ToLocalizedString());
stringBuilder.AppendLine("Phonetic name (Short): " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Short));
stringBuilder.AppendLine("Phonetic name (Medium): " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Medium));
stringBuilder.AppendLine("Phonetic name (Long): " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Long));
stringBuilder.AppendLine("Phonetic name (Abbreviated): " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Abbreviated));
}
}
}
}
public void ClickYkLoginBtn()
{
Debug.Log("YouKe Login Clicked");
}
}