You are on page 1of 2

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;
using Facebook.Unity;
using Firebase;
using Firebase.Auth;
using UnityEngine.UI;

public class facebookAuthenticator : MonoBehaviour


{
public Text userID;
public GameObject CanvasIntroMenu, CanvasIntroduccion;
public GameObject Panel;
public GameObject PanelError;
private FirebaseAuth facebookAuth;

private void Awake()


{
if (!FB.IsInitialized)
FB.Init();
else
FB.ActivateApp();
}

public void logIn()


{
FB.LogInWithReadPermissions(callback: onLogIn);
}

private void onLogIn(ILoginResult result)


{
if (FB.IsLoggedIn)
{
Debug.Log("FB is logged in");
AccessToken tocken = AccessToken.CurrentAccessToken;
//userID.text = tocken.UserId;
Firebase.Auth.Credential credential = Firebase.Auth.FacebookAuthProvi
der.GetCredential(tocken.TokenString);
Debug.Log("FB token: " + tocken);
accessToken(credential);

}
else{
Debug.Log("log failed");
Panel.SetActive(false);
PanelError.SetActive(true);
}
}

public void accessToken(Credential firebaseResult)


{
FirebaseAuth auth = FirebaseAuth.DefaultInstance;

//if (FB.IsLoggedIn){
// return;
//}

FirebaseAuth.DefaultInstance.SignInWithCredentialAsync(firebaseResult).
ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("SignInWithCredentialAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("SignInWithCredentialAsync encountered an error: "
+ task.Exception);
return;
}

Firebase.Auth.FirebaseUser newUser = task.Result;


Debug.LogFormat("User signed in successfully: {0} ({1})", newUser.Disp
layName, newUser.UserId);
CanvasIntroMenu.SetActive(false);
CanvasIntroduccion.SetActive(true);
});
}
}

You might also like