Added firebase and playfab sign in
parent
e0de8ccb2a
commit
0e6fd6b319
@ -0,0 +1,102 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 45ec4119cd173084d8102fa5bb179386
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue