@ -3,36 +3,31 @@ using UnityEngine.UI;
using Google ;
using PlayFab ;
using PlayFab.ClientModels ;
using System ;
using System.Collections.Generic ;
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 Bootstrapper bootstrapper ; // Assign 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
RequestAuthCode = true
} ;
GoogleSignIn . Configuration = configuration ;
Application . quitting + = SyncPlayerPrefsToPlayFabOnQuit ;
}
void Start ( )
{
if ( PlayerPrefs . HasKey ( "PlayFabID" ) )
{
Debug . Log ( "User already signed in, attempting silent login..." ) ;
@ -42,51 +37,62 @@ public class GoogleSignInManager : MonoBehaviour
{
Debug . Log ( "No saved PlayFab ID, waiting for user sign-in." ) ;
googleSignInButton . gameObject . SetActive ( true ) ;
googleSignInButton . onClick . RemoveAllListeners ( ) ;
googleSignInButton . onClick . AddListener ( SignInWithGoogle ) ;
SignInWithGoogle ( ) ;
}
}
public void SignInWithGoogle ( )
{
googleSignInButton . interactable = false ;
GoogleSignIn . DefaultInstance . SignIn ( ) . ContinueWith ( OnGoogleSignIn ) ;
}
private void OnGoogleSignIn ( Task < GoogleSignInUser > task )
{
if ( task . IsFaulted )
if ( task . IsFaulted | | task . IsCanceled )
{
Debug . LogError ( "Google Sign-In encountered an error: " + task . Exception ) ;
Debug . LogError ( "Google Sign-In failed: " + task . Exception ) ;
// ✅ Show the sign-in button again
googleSignInButton . gameObject . SetActive ( true ) ;
googleSignInButton . interactable = true ;
googleSignInButton . onClick . RemoveAllListeners ( ) ;
googleSignInButton . onClick . AddListener ( SignInWithGoogle ) ;
return ;
}
else if ( task . IsCanceled )
GoogleSignInUser user = task . Result ;
string authCode = user . AuthCode ;
PlayerPrefs . SetString ( "GoogleAuthCode" , authCode ) ;
PlayerPrefsKeys . RegisterKey ( "GoogleAuthCode" ) ;
PlayerPrefs . Save ( ) ;
LoginToPlayFab ( authCode ) ;
}
private void SignInSilently ( )
{
if ( PlayerPrefs . HasKey ( "GoogleAuthCode" ) )
{
Debug . Log ( "Google Sign-In was canceled." ) ;
string authCode = PlayerPrefs . GetString ( "GoogleAuthCode" ) ;
LoginToPlayFab ( authCode ) ;
}
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.
Debug . LogWarning ( "No Google AuthCode found for silent sign-in." ) ;
googleSignInButton . gameObject . SetActive ( true ) ;
googleSignInButton . onClick . RemoveAllListeners ( ) ;
googleSignInButton . onClick . AddListener ( SignInWithGoogle ) ;
}
}
}
private void LoginToPlayFab ( string idToken )
private void LoginToPlayFab ( string authCode )
{
var request = new LoginWithGoogleAccountRequest
{
TitleId = PlayFabSettings . TitleId ,
ServerAuthCode = idToken ,
ServerAuthCode = authCode ,
CreateAccount = true
} ;
@ -95,47 +101,85 @@ public class GoogleSignInManager : MonoBehaviour
private void OnPlayFabLoginSuccess ( LoginResult result )
{
Debug . Log ( " PlayFab Login Success! PlayFab ID: " + result . PlayFabId ) ;
Debug . Log ( " ✅ PlayFab Login Success! PlayFab ID: " + result . PlayFabId ) ;
// Save PlayFab ID to keep the user signed in automatically
PlayerPrefs . SetString ( "PlayFabID" , result . PlayFabId ) ;
PlayerPrefsKeys . RegisterKey ( "PlayFabID" ) ;
PlayerPrefs . Save ( ) ;
// Start the game after login
bootstrapper . StartGame ( ) ;
LoadPlayerPrefsFromPlayFab ( ( ) = >
{
googleSignInButton . gameObject . SetActive ( false ) ;
bootstrapper . StartGame ( ) ; // Start the game after loading
} ) ;
}
private void OnPlayFabLoginFailure ( PlayFabError error )
{
Debug . LogError ( "PlayFab Login Failed: " + error . GenerateErrorReport ( ) ) ;
Debug . LogError ( "❌ PlayFab Login Failed: " + error . GenerateErrorReport ( ) ) ;
// ✅ Re-enable button so player can retry
googleSignInButton . gameObject . SetActive ( true ) ;
googleSignInButton . interactable = true ;
googleSignInButton . onClick . RemoveAllListeners ( ) ;
googleSignInButton . onClick . AddListener ( SignInWithGoogle ) ;
}
private void SignInSilently ( )
private void LoadPlayerPrefsFromPlayFab( Action onComplete )
{
if ( PlayerPrefs . HasKey ( "GoogleIdToken" ) )
PlayFabClientAPI . GetUserData ( new GetUserDataRequest ( ) , result = >
{
Debug . Log ( "Attempting silent Google Sign-In..." ) ;
string savedIdToken = PlayerPrefs . GetString ( "GoogleIdToken" ) ;
LoginToPlayFab ( savedIdToken ) ; // Skip Google UI, go directly to PlayFab login
}
else
if ( result . Data ! = null )
{
foreach ( var entry in result . Data )
{
PlayerPrefs . SetString ( entry . Key , entry . Value . Value ) ;
PlayerPrefsKeys . RegisterKey ( entry . Key ) ;
}
PlayerPrefs . Save ( ) ;
Debug . Log ( "✅ Loaded PlayerPrefs from PlayFab." ) ;
}
else
{
Debug . Log ( "ℹ ️ No saved PlayerPrefs found in PlayFab." ) ;
}
onComplete ? . Invoke ( ) ;
} ,
error = >
{
Debug . LogWarning ( "No Google ID Token saved, user must sign in manually." ) ;
Debug . LogError ( "❌ Failed to load PlayerPrefs from PlayFab: " + error . GenerateErrorReport ( ) ) ;
onComplete ? . Invoke ( ) ;
} ) ;
}
private void SyncPlayerPrefsToPlayFabOnQuit ( )
{
Dictionary < string , string > allPrefs = new Dictionary < string , string > ( ) ;
foreach ( var key in PlayerPrefsKeys . GetAllKeys ( ) )
{
allPrefs [ key ] = PlayerPrefs . GetString ( key ) ;
}
var request = new UpdateUserDataRequest
{
Data = allPrefs
} ;
PlayFabClientAPI . UpdateUserData ( request ,
result = > Debug . Log ( "✅ Synced PlayerPrefs to PlayFab on quit." ) ,
error = > Debug . LogError ( "❌ Failed to sync PlayerPrefs on quit: " + error . GenerateErrorReport ( ) ) ) ;
}
public void SignOut ( )
{
Debug . Log ( "Signing out user..." ) ;
// Clear stored login data
PlayerPrefs . DeleteKey ( "PlayFabID" ) ;
PlayerPrefs . DeleteKey ( "GoogleIdToken" ) ;
PlayerPrefs . DeleteKey ( "GoogleAuthCode" ) ;
PlayerPrefs . Save ( ) ;
// Sign out from Google
GoogleSignIn . DefaultInstance . SignOut ( ) ;
Debug . Log ( "User signed out. They will need to log in again." ) ;
Debug . Log ( "User signed out." ) ;
}
}