0% found this document useful (0 votes)
4 views4 pages

9

Uploaded by

namitsharma549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

9

Uploaded by

namitsharma549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System.Collections.

Generic;
using Firebase.Extensions;
using Google;
using System.Threading.Tasks;
using UnityEngine;
using TMPro;
using Firebase.Auth;
using UnityEngine.UI;
using UnityEngine.SceneManagement; // Required for scene management
using UnityEngine.Networking;
using System.Collections;

public class LoginWithGoogle : MonoBehaviour


{
public string GoogleAPI = "1031308617646-
8bdqemcs3bmf1bae8tvk1d38d0tvie93.apps.googleusercontent.com";
private GoogleSignInConfiguration configuration;

private Firebase.Auth.FirebaseAuth auth;

public TMP_Text Username, UserEmail;


public GameObject LoginScreen, ProfileScreen;
public Image UserProfilePic;
public Button ContinueButton;

public GameObject BackgroundImage; // Reference to the background image

private void Awake()


{
configuration = new GoogleSignInConfiguration
{
WebClientId = GoogleAPI,
RequestIdToken = true,
};
}

private void Start()


{
InitFirebase();

// Initially disable the button, profile picture, and background image until login is successful
ContinueButton.gameObject.SetActive(false);
UserProfilePic.gameObject.SetActive(false);
BackgroundImage.SetActive(false); // Hide background image at start

ContinueButton.onClick.AddListener(ChangeScene); // Assign the button to change the scene


}

void InitFirebase()
{
auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
}

public void Login()


{
GoogleSignIn.Configuration = new GoogleSignInConfiguration
{
WebClientId = GoogleAPI, // Client ID from Firebase Console (Web Client ID)
RequestIdToken = true,
};
GoogleSignIn.Configuration.RequestEmail = true;

Task<GoogleSignInUser> signIn = GoogleSignIn.DefaultInstance.SignIn();

signIn.ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.Log("Sign-in was canceled.");
}
else if (task.IsFaulted)
{
Debug.LogError("Sign-in encountered an error: " + task.Exception);
}
else
{
Debug.Log("Sign-in successful.");
GoogleSignInUser googleUser = task.Result; // Get the Google user
Credential credential =
Firebase.Auth.GoogleAuthProvider.GetCredential(googleUser.IdToken, null);
auth.SignInWithCredentialAsync(credential).ContinueWith(authTask =>
{
if (authTask.IsCanceled)
{
Debug.Log("Firebase Authentication was canceled.");
}
else if (authTask.IsFaulted)
{
Debug.LogError("Firebase Authentication encountered an error: " +
authTask.Exception);
}
else
{
FirebaseUser user = authTask.Result; // Get the Firebase user
Debug.Log("User signed in successfully: " + user.DisplayName);

// Update UI in a coroutine to ensure graphics device is ready


StartCoroutine(UpdateUIAfterLogin(user));
}
});
}
});
}

private IEnumerator UpdateUIAfterLogin(FirebaseUser user)


{
yield return null; // Wait for a frame to ensure everything is initialized

// Update UI
Username.text = user.DisplayName;
UserEmail.text = user.Email;

// Fetch and display profile picture


StartCoroutine(LoadProfilePicture(user.PhotoUrl.ToString()));

// Hide login screen and show profile screen


LoginScreen.SetActive(false);
ProfileScreen.SetActive(true);

// Enable the button, profile picture, and background image after login is successful
ContinueButton.gameObject.SetActive(true);
UserProfilePic.gameObject.SetActive(true);
BackgroundImage.SetActive(true); // Show the background image
}

// Function to change the scene


void ChangeScene()
{
SceneManager.LoadScene("StartingScreen"); // Replace with the actual scene name
}

IEnumerator LoadProfilePicture(string imageUrl)


{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(imageUrl);
yield return request.SendWebRequest();

if (request.result == UnityWebRequest.Result.ConnectionError || request.result ==


UnityWebRequest.Result.ProtocolError)
{
Debug.LogError("Error downloading profile picture: " + request.error);
}
else
{
Texture2D texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
UserProfilePic.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),
new Vector2(0.5f, 0.5f));
}
}
}

You might also like