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.
PlumberUltimateAds/Assets/GoogleSignIn/GoogleSignInController.cs

51 lines
1.6 KiB
C#

using UnityEngine;
using Google;
using System.Threading.Tasks;
public class GoogleSignInController : MonoBehaviour
{
private GoogleSignInConfiguration configuration;
void Awake()
{
// Set up the Google Sign-In configuration
configuration = new GoogleSignInConfiguration
{
WebClientId = "624507103444-6agok4g1q29bsb615v235jbf0k585ruk.apps.googleusercontent.com",
RequestEmail = true,
RequestIdToken = false, // ❌ We no longer request IdToken, since we use ServerAuthCode instead
RequestAuthCode = true // ✅ Enable ServerAuthCode for secure backend authentication
};
GoogleSignIn.Configuration = configuration;
}
public void SignInWithGoogle()
{
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnGoogleSignIn);
}
private void OnGoogleSignIn(Task<GoogleSignInUser> task)
{
if (task.IsFaulted)
{
Debug.LogError("Google Sign-In encountered an error: " + task.Exception);
}
else if (task.IsCanceled)
{
Debug.Log("Google Sign-In was canceled.");
}
else
{
GoogleSignInUser user = task.Result;
Debug.Log("✅ Google Sign-In succeeded!");
Debug.Log("👤 Display Name: " + user.DisplayName);
Debug.Log("📧 Email: " + user.Email);
Debug.Log("🔑 Server Auth Code: " + user.AuthCode);
// 🔹 Send this AuthCode to your backend (PlayFab, Firebase, or custom server)
// The backend will exchange this for an access token.
}
}
}