Firebase Facebook Auth Integration in Unity Game Development

Ashish Ramtekkar
5 min readJul 9, 2020

--

Below are the steps that help to integrate the firebase Facebook auth integration in Unity Game

  1. Create a Firebase project & register the unity application in the firebase console. (https://console.firebase.google.com/)
  2. Create an app in the developer's Facebook portal(https://developers.facebook.com/)
  3. Configure the Facebook info in Firebase
  4. Download & configure the unity packages of firebase & Facebook.
  5. Code snippet for Firebase Facebook AUTH.

Create a Firebase project

Login to https://console.firebase.google.com/ with your Gmail ID

Create a new project in firebase

Adding a project in firebase is very simple, just kind of formality we have to follow by giving project name and selection of analytical feature at the time of adding a project in firebase.

A simple video that showcase of creation of firebase project in firebase

Register the unity application in the firebase console

If you’re releasing your game on both iOS and Android, register both build targets of your Unity project with the same Firebase project. If you have multiple build variants with different iOS bundle IDs or Android app IDs defined, you must register each variant with the same Firebase project.

  1. Click the Unity icon to launch the setup workflow.
  2. If you’ve already added an app to your Firebase project, click Add app to display the platform options.
  3. Select which build target of your Unity project that you’d like to register, or you can even select to register both targets now at the same time.
  4. Enter your Unity project’s platform-specific ID(s).
  • For iOS — Enter your Unity project’s iOS ID in the iOS bundle ID field.
  • For Android — Enter your Unity project’s Android ID in the Android package name field.

Note: Make sure that you enter the ID that your project is actually using. The ID value is case-sensitive, and it cannot be changed for these Firebase apps after they’re registered with your Firebase project.

Create an app in the developer’s Facebook

Create a new app by providing the essential information in the portal (https://developers.facebook.com/)

Note: Make sure we will be using the new .keystore and use the same to register the app in the Facebook developer portal.

a formality to register the app in the Facebook developer portal.

The above steps give the proper information once you expand each step.

Configure the Facebook info in Firebase

The Facebook application gives the AppID & App secret key, that has to provide in the firebase app.

Authentication -> Sign in-method -> (option) facebook

Follow the other steps to complete set up by clicking on learn more in firebase.

Download the unity packages of firebase & Facebook.

Manual Download the unity packages https://firebase.google.com/download/unity

Note: Download Facebook packages while registering the app in the Facebook app.

Configure the packages in Unity IDE

Once you have downloaded the packages, just double click on packages (Firebase Unity SDK (specifically, FirebaseAuth.unitypackage & facebook-unity-sdk-x.x.x.unitypackage) ) that will automatically open in unity and will be linked to open project in unity.

Note: Make sure to keep internet connection ON for downloading dependencies that require by packages. eg. Gradle etc.

Force Resolve to download the dependencies for android

This will help to download the dependencies.

Note : Keep you machine connected to Internet to download dependencies.

Initialize the Facebook SDK

// Include Facebook packageusing Firebase.Auth;using Facebook.Unity;
// Start function from Unity's MonoBehavior
void Start ()
{
if (!FB.IsInitialized) {
FB.Init(initCallback, onHideUnity);
} else {
// Already initialized
FB.ActivateApp();
}
}

private void initCallback ()
{
if (FB.IsInitialized) {
// Signal an app activation App Event
FB.ActivateApp();
// Continue with Facebook SDK
// ...
} else {
Debug.Log("Something went wrong to Initialize the Facebook SDK");
}
}

private void onHideUnity (bool isGameScreenShown)
{
if (!isGameScreenShown) {
// Pause the game - we will need to hide
Time.timeScale = 0;
} else {
// Resume the game - we're getting focus again
Time.timeScale = 1;
}
}
private void loginBtnForFB (){
// Permission option list https://developers.facebook.com/docs/facebook-login/permissions/
var permissons = new List<string>(). {"email","user_birthday","user_friends, ,"public_profile"}; FB.LogInWithReadPermissions(permissons, authStatusCallback);}private void authStatusCallback (ILoginResult result) {
if (FB.IsLoggedIn) {
// AccessToken class will have session details
var accessToken = Facebook.Unity.AccessToken.CurrentAccessToken;
// current access token's User ID : aToken.UserId
loginviaFirebaseFacebook(accessToken);

} else {
Debug.Log("User cancelled login");
}
}

Sending accessToken from Facebook SDK to firebase call

Firebase.Auth.FirebaseAuth auth;
private void loginviaFirebaseFacebook (String accessToken){
auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
Firebase.Auth.Credential credential =
Firebase.Auth.FacebookAuthProvider.GetCredential(accessToken);
auth.SignInWithCredentialAsync(credential).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.DisplayName, newUser.UserId);
});
}

Getting Profile Data

function getProfileData (){
Firebase.Auth.FirebaseUser user = auth.CurrentUser;
if (user != null) {
string name = user.DisplayName;
string email = user.Email;
System.Uri photo_url = user.PhotoUrl;
// The user's Id, unique to the Firebase project.
string uid = user.UserId;
}
}

Logout

function logout(){
auth.SignOut();
}

Thanks and keep in touch! THOKO TAALI & ENJOY CODING :)

--

--