using UnityEngine ;
using UnityEngine.UI ;
using Google ;
using PlayFab ;
using PlayFab.ClientModels ;
using System.Threading.Tasks ;
public class GoogleSignInManager : MonoBehaviour
{
//#if UNITY_IOS
// private string webClientId = "723833850517-2j378fbm2a8u644p4qqod4qbi93dsgom.apps.googleusercontent.com"; // Replace with your actual Google Web Client ID
//#elif UNITY_ANDROID
// private string webClientId = "723833850517-tfaer77vetml1bhv9qbj3o84ec52u45d.apps.googleusercontent.com"; // Replace with your actual Google Web Client ID
//#endif
//private string webClientId = "723833850517-865419enf8t0j1itln3cgmd1b67shsue.apps.googleusercontent.com";
private GoogleSignInConfiguration configuration ;
public Bootstrapper bootstrapper ; // Assign Bootstrapper in Inspector
public Button googleSignInButton ;
void Awake ( )
{
// Set up the Google Sign-In configuration
configuration = new GoogleSignInConfiguration
{
WebClientId = "723833850517-865419enf8t0j1itln3cgmd1b67shsue.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 ;
}
void Start ( )
{
if ( PlayerPrefs . HasKey ( "PlayFabID" ) )
{
Debug . Log ( "User already signed in, attempting silent login..." ) ;
SignInSilently ( ) ;
}
else
{
Debug . Log ( "No saved PlayFab ID, waiting for user sign-in." ) ;
googleSignInButton . gameObject . SetActive ( true ) ;
googleSignInButton . onClick . AddListener ( SignInWithGoogle ) ;
SignInWithGoogle ( ) ;
}
}
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 ) ;
string idToken = user . AuthCode ;
PlayerPrefs . SetString ( "GoogleIdToken" , idToken ) ; // Save Google Token for auto-login
PlayerPrefs . Save ( ) ;
Debug . Log ( "Google Sign-In Successful! Fetching PlayFab login..." ) ;
LoginToPlayFab ( idToken ) ;
// 🔹 Send this AuthCode to your backend (PlayFab, Firebase, or custom server)
// The backend will exchange this for an access token.
}
}
private void LoginToPlayFab ( string idToken )
{
var request = new LoginWithGoogleAccountRequest
{
TitleId = PlayFabSettings . TitleId ,
ServerAuthCode = idToken ,
CreateAccount = true
} ;
PlayFabClientAPI . LoginWithGoogleAccount ( request , OnPlayFabLoginSuccess , OnPlayFabLoginFailure ) ;
}
private void OnPlayFabLoginSuccess ( LoginResult result )
{
Debug . Log ( "PlayFab Login Success! PlayFab ID: " + result . PlayFabId ) ;
// Save PlayFab ID to keep the user signed in automatically
PlayerPrefs . SetString ( "PlayFabID" , result . PlayFabId ) ;
PlayerPrefs . Save ( ) ;
// Start the game after login
bootstrapper . StartGame ( ) ;
}
private void OnPlayFabLoginFailure ( PlayFabError error )
{
Debug . LogError ( "PlayFab Login Failed: " + error . GenerateErrorReport ( ) ) ;
}
private void SignInSilently ( )
{
if ( PlayerPrefs . HasKey ( "GoogleIdToken" ) )
{
Debug . Log ( "Attempting silent Google Sign-In..." ) ;
string savedIdToken = PlayerPrefs . GetString ( "GoogleIdToken" ) ;
LoginToPlayFab ( savedIdToken ) ; // Skip Google UI, go directly to PlayFab login
}
else
{
Debug . LogWarning ( "No Google ID Token saved, user must sign in manually." ) ;
}
}
public void SignOut ( )
{
Debug . Log ( "Signing out user..." ) ;
// Clear stored login data
PlayerPrefs . DeleteKey ( "PlayFabID" ) ;
PlayerPrefs . DeleteKey ( "GoogleIdToken" ) ;
PlayerPrefs . Save ( ) ;
// Sign out from Google
GoogleSignIn . DefaultInstance . SignOut ( ) ;
Debug . Log ( "User signed out. They will need to log in again." ) ;
}
}