0% found this document useful (0 votes)
43 views2 pages

Facebook

This C# script handles authentication with Facebook and Firebase. It initializes Facebook login and uses the Facebook access token to sign in to Firebase. On successful Facebook login, it passes the Facebook credential to the accessToken method. This method signs in to Firebase using the credential and handles the callback, displaying a success message if sign in is successful.

Uploaded by

John Ceb
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)
43 views2 pages

Facebook

This C# script handles authentication with Facebook and Firebase. It initializes Facebook login and uses the Facebook access token to sign in to Firebase. On successful Facebook login, it passes the Facebook credential to the accessToken method. This method signs in to Firebase using the credential and handles the callback, displaying a success message if sign in is successful.

Uploaded by

John Ceb
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/ 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