- Home /
firebase facebook authenitaction
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? thanks
You do need a "sign in with facebook" button and add the logIn() function to it.
Your answer
Follow this Question
Related Questions
Unity Authentication (UAS) - SignInWithFacebookAsync Permission Denied 1 Answer
Unity web player problems with facebook 0 Answers
Need to Authenticate users with Google, FB and the like on a UWP project 0 Answers
Facebook SDK doesn't work on google play store 0 Answers
Facebook login dialogue won't appear in VR projects 1 Answer