You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.0 KiB
C#
102 lines
3.0 KiB
C#
1 month ago
|
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;
|
||
|
|
||
|
private void OnEnable()
|
||
|
{
|
||
|
googleSignInButton.onClick.AddListener(SignInWithGoogle);
|
||
|
}
|
||
|
|
||
|
private void OnDisable()
|
||
|
{
|
||
|
googleSignInButton.onClick.RemoveListener(SignInWithGoogle);
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
// 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");
|
||
|
|
||
|
// PlayFab uses ServerAuthCode
|
||
|
var request = new LoginWithGoogleAccountRequest
|
||
|
{
|
||
|
TitleId = PlayFabSettings.staticSettings.TitleId,
|
||
|
ServerAuthCode = task.Result.AuthCode,
|
||
|
CreateAccount = true
|
||
|
};
|
||
|
|
||
|
PlayFabClientAPI.LoginWithGoogleAccount(request, OnPlayFabLoginSuccess, OnPlayFabLoginFailure);
|
||
|
}
|
||
|
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();
|
||
|
|
||
|
}
|
||
|
|
||
|
private void OnPlayFabLoginFailure(PlayFabError error)
|
||
|
{
|
||
|
Debug.LogError("PlayFab Login Failed: " + error.GenerateErrorReport());
|
||
|
|
||
|
googleSignInButton.gameObject.SetActive(true);
|
||
|
googleSignInButton.interactable = true;
|
||
|
}
|
||
|
|
||
|
}
|