EndlessRocket/Assets/Scripts/AppleLogin.cs
2023-08-06 16:46:05 +05:30

93 lines
2.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Text;
using AppleAuth;
using AppleAuth.Enums;
using AppleAuth.Interfaces;
using AppleAuth.Native;
using UnityEngine;
public class AppleLogin : MonoBehaviour
{
public GoogleLoginManager googleLogin;
IAppleAuthManager m_AppleAuthManager;
public string Token { get; private set; }
public string Error { get; private set; }
public void Initialize()
{
var deserializer = new PayloadDeserializer();
m_AppleAuthManager = new AppleAuthManager(deserializer);
}
public void Update()
{
if (m_AppleAuthManager != null)
{
m_AppleAuthManager.Update();
}
}
public void LoginToApple()
{
// Initialize the Apple Auth Manager
if (m_AppleAuthManager == null)
{
Initialize();
}
// Set the login arguments
var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
// Perform the login
m_AppleAuthManager.LoginWithAppleId(
loginArgs,
credential =>
{
var appleIDCredential = credential as IAppleIDCredential;
if (appleIDCredential != null)
{
var idToken = Encoding.UTF8.GetString(
appleIDCredential.IdentityToken,
0,
appleIDCredential.IdentityToken.Length);
Debug.Log("Sign-in with Apple successfully done. IDToken: " + idToken);
Token = idToken;
if (appleIDCredential == null)
{
MessageDialog.instance.ShowMessage("Error", "Failed to login using Apple ID.\nError Code: A-00");
}else if(appleIDCredential.Email == null)
{
MessageDialog.instance.ShowMessage("Error", "Failed to login using Apple ID.\nError Code: A-0E");
}
else if (appleIDCredential.FullName == null)
{
MessageDialog.instance.ShowMessage("Error", "Failed to login using Apple ID.\nError Code: A-0U");
}
var fullname = appleIDCredential.Email;
Debug.Log("Email : " + appleIDCredential.Email);
Debug.Log("User :" + fullname);
googleLogin.OnAppleSigninDone(fullname, appleIDCredential.Email);
}
else
{
Debug.Log("Sign-in with Apple error. Message: appleIDCredential is null");
Error = "Retrieving Apple Id Token failed.";
}
},
error =>
{
Debug.Log("Sign-in with Apple error. Message: " + error);
Error = "Retrieving Apple Id Token failed.";
}
);
}
}