firebase authentication with facebook
hi unity community
i am trying to add firebase authentication into my game, but the documentation in the firebase site is a bit confusing. i found this code on the unity forums:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Facebook.Unity;
using Firebase.Auth;
using UnityEngine.UI;
public class facebookAuthenticator : MonoBehaviour
{
public Text userID;
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)
{
AccessToken tocken = AccessToken.CurrentAccessToken;
userID.text = tocken.UserId;
Credential credential = FacebookAuthProvider.GetCredential(tocken.TokenString);
}
else
Debug.Log("log failed");
}
public void accessToken(Credential firebaseResult)
{
FirebaseAuth auth = FirebaseAuth.DefaultInstance;
if (FB.IsLoggedIn)
return;
auth.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;
}
FirebaseUser newUser = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
});
}
}
by all accounts on the feed, the code works. however, i do not understand how i would invoke this in my game? how can i get users to fill in their info and pass it to this? do i need a button saying "sign in with facebook" and link it to this script? please help.
Answer by GamerBoxStudios · Apr 06, 2018 at 09:28 AM
it seems that you need to add the method logIn()
to your "Sign in with facebook" button listener. I haven't seen the rest of the code. but hey I am having a problem in building my project!! because of the google jar resolver.. can you help me with that!?
@GamerBoxStudios Just click on the Assets tab (beside Edit tab) in Unity. Then: Play Services Resolver -> Android Resolver -> Settings... From the Settings dialog box, uncheck the options "Enable Auto-Resolution" and "Auto-resolution disabled warning".... That should solve your problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220612132446im_/https://answers.unity.com/themes/thub/images/avi.jpg)