Paul Kumar
Paul Kumar

Reputation: 29

Unity firebase facebbok authentication

I do not understand the documenation on the firebase site with regards to the unity firebase facebook authentication.

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);
    });
}

}

I am confused on how I would invoke this. Are there steps I must take before executing this code? how do I call it? Do i simply attach it to a button and give the user input fields to fill out? Please help, thanks.

Upvotes: 0

Views: 988

Answers (2)

Lad
Lad

Reputation: 91

First of all you should receive access token from callback result. Secondly you should invoke firebase auth method after you received the token by accessToken(credential); following your code, so your onLogin method should be like this

private void onLogIn(ILoginResult result)
{
   if (FB.IsLoggedIn)
   {
      AccessToken tocken = result.AccessToken;//received access token
      userID.text = tocken.UserId;
      Credential credential = FacebookAuthProvider.GetCredential(tocken.TokenString);
      accessToken(credential);//invoke auth method
   }
   else
      Debug.Log("log failed");
}

Upvotes: 0

David G
David G

Reputation: 1

I'm doing something very similar to this and yes, you pretty much have to asign the logIn function into a button so that once it's clicked it begins the Facebook auth, but for the firebase part you have to get the access token and that is done another way. I suggest looking into something like this: http://greyzoned.com/tutorials/facebook-sdk-7-3-0-in-unity-5-3-login-username-profile-picture-tutorial/ to the idea of how to do it. Good luck!

Upvotes: 0

Related Questions