|
|
|
using System;
|
|
|
|
using Firebase.Auth;
|
|
|
|
using Google;
|
|
|
|
using UnityEngine;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Firebase.Extensions;
|
|
|
|
using PlayFab;
|
|
|
|
using PlayFab.ClientModels;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
public class GoogleSignInHandler : MonoBehaviour
|
|
|
|
{
|
|
|
|
private FirebaseAuth auth;
|
|
|
|
private GoogleSignInConfiguration configuration;
|
|
|
|
[SerializeField] private Button googleSignInButton;
|
|
|
|
[SerializeField] private Button guestLoginButton;
|
|
|
|
[SerializeField] private Bootstrapper bootstrapper;
|
|
|
|
private void OnEnable()
|
|
|
|
{
|
|
|
|
googleSignInButton.onClick.AddListener(SignInWithGoogle);
|
|
|
|
guestLoginButton.onClick.AddListener(GuestLogin);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
{
|
|
|
|
googleSignInButton.onClick.RemoveListener(SignInWithGoogle);
|
|
|
|
guestLoginButton.onClick.RemoveListener(GuestLogin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
PlayFabSettings.staticSettings.TitleId = GameConstants.PlayfabTitleId;
|
|
|
|
auth = FirebaseAuth.DefaultInstance;
|
|
|
|
configuration = new GoogleSignInConfiguration
|
|
|
|
{
|
|
|
|
WebClientId = GameConstants.WebClientID,
|
|
|
|
RequestEmail = true,
|
|
|
|
RequestAuthCode = true,
|
|
|
|
RequestIdToken = true
|
|
|
|
};
|
|
|
|
GoogleSignIn.Configuration = configuration;
|
|
|
|
GoogleSignIn.DefaultInstance.EnableDebugLogging(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void GuestLogin()
|
|
|
|
{
|
|
|
|
Debug.Log("Starting game in Guest Mode...");
|
|
|
|
SafePlayerPrefs.SetInt("GuestMode", 1);
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
|
|
|
googleSignInButton.gameObject.SetActive(false);
|
|
|
|
guestLoginButton.gameObject.SetActive(false);
|
|
|
|
|
|
|
|
PlayFabSettings.staticSettings.TitleId = GameConstants.PlayfabTitleId;
|
|
|
|
|
|
|
|
string customId = SystemInfo.deviceUniqueIdentifier;
|
|
|
|
SafePlayerPrefs.SetString("GuestCustomID", customId); // Save customId
|
|
|
|
|
|
|
|
var request = new LoginWithCustomIDRequest
|
|
|
|
{
|
|
|
|
CustomId = customId,
|
|
|
|
CreateAccount = true
|
|
|
|
};
|
|
|
|
|
|
|
|
PlayFabClientAPI.LoginWithCustomID(request, result =>
|
|
|
|
{
|
|
|
|
Debug.Log("✅ Guest Login Success! PlayFab ID: " + result.PlayFabId);
|
|
|
|
SafePlayerPrefs.SetString("PlayFabID", result.PlayFabId);
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
|
|
|
if (result.NewlyCreated)
|
|
|
|
{
|
|
|
|
SafePlayerPrefs.SetInt("NeedsDisplayNameUpload", 1);
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
bootstrapper.StartGame();
|
|
|
|
},
|
|
|
|
error =>
|
|
|
|
{
|
|
|
|
Debug.LogError("❌ Guest PlayFab login failed: " + error.GenerateErrorReport());
|
|
|
|
googleSignInButton.gameObject.SetActive(true);
|
|
|
|
guestLoginButton.gameObject.SetActive(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SignInWithGoogle()
|
|
|
|
{
|
|
|
|
if (GoogleSignIn.Configuration == null)
|
|
|
|
{
|
|
|
|
Debug.LogWarning("Google Sign-In Configuration is not set.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnGoogleSignIn);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnGoogleSignIn(Task<GoogleSignInUser> task)
|
|
|
|
{
|
|
|
|
if (task.IsFaulted || task.IsCanceled || task.Result == null)
|
|
|
|
{
|
|
|
|
Debug.LogError("Google Sign-In failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PlayFab uses ServerAuthCode
|
|
|
|
var request = new LoginWithGoogleAccountRequest
|
|
|
|
{
|
|
|
|
TitleId = PlayFabSettings.staticSettings.TitleId,
|
|
|
|
ServerAuthCode = task.Result.AuthCode,
|
|
|
|
CreateAccount = true
|
|
|
|
};
|
|
|
|
Debug.Log($"LoginWithGoogleAccount Title {request.TitleId} AuthCode {request.ServerAuthCode}");
|
|
|
|
PlayFabClientAPI.LoginWithGoogleAccount(request, OnPlayFabLoginSuccess, OnPlayFabLoginFailure);
|
|
|
|
// // Firebase uses ID token
|
|
|
|
// var firebaseCredential = GoogleAuthProvider.GetCredential(task.Result.IdToken, null);
|
|
|
|
// FirebaseAuth.DefaultInstance.SignInWithCredentialAsync(firebaseCredential).ContinueWith(authTask =>
|
|
|
|
// {
|
|
|
|
// if (authTask.IsCompleted && !authTask.IsFaulted)
|
|
|
|
// {
|
|
|
|
// Debug.Log("Firebase sign-in successful");
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// Debug.LogError("Firebase sign-in failed: " + authTask.Exception);
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
private void OnPlayFabLoginSuccess(LoginResult result)
|
|
|
|
{
|
|
|
|
Debug.Log("PlayFab Login Success! PlayFab ID: " + result.PlayFabId);
|
|
|
|
|
|
|
|
SafePlayerPrefs.SetString("PlayFabID", result.PlayFabId);
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
bootstrapper.StartGame();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnPlayFabLoginFailure(PlayFabError error)
|
|
|
|
{
|
|
|
|
Debug.LogError("PlayFab Login Failed: " + error.GenerateErrorReport());
|
|
|
|
|
|
|
|
googleSignInButton.gameObject.SetActive(true);
|
|
|
|
googleSignInButton.interactable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|