This document is about: FUSION 2
SWITCH TO

Facebook Authentication

For Facebook Authentication, clients will first use Meta's API to authenticate the user, then send the resulting secret token to Photon to confirm and use their identity.

This page describes the setup and workflow.

Server Side

Facebook App Setup

First, you need to create a Facebook application if you don't have one already.

  • Go to the Facebook Developers website and login.
  • Click Apps -> Create a New App , enter the name of your app and press Create App button.
  • Choose Apps -> [your_app] and copy App ID and App Secret.

Photon Configuration

Open the Photon Cloud Dashboard, and find the application to setup.
Click "Manage" and find the "Authentication" section, which shows the currently configured authentication providers.

Add a provider for "Facebook" and fill in the two required parameters:

  • appid = this refers to your Facebook App ID
  • secret = your Facebook App Secret

Unlike for Custom Authentication, the Authentication URL is fixed and set automatically.

Save changes.

Client Side

On the client side, apps first have to authenticate via Facebook to get a token, which is sent to the Photon server in the AuthenticationValues.

In Unity, you will need to:

  • Import Facebook SDK for Unity.
  • In Unity's main menu go to Facebook -> Edit Settings, enter the App Name and App Id for your Facebook Application.

Implementation

Create a new MonoBehaviour, attach it to an object on scene and add the following code for Facebook initialization and login:

C#

// Include Facebook namespace
using Facebook.Unity;

// [..]

private void Awake()
{
    if (!FB.IsInitialized)
    {
        // Initialize the Facebook SDK
        FB.Init(InitCallback);
    }
    else
    {
        FacebookLogin();
    }
}

private void InitCallback()
{
    if (FB.IsInitialized)
    {
        FacebookLogin();
    }
    else
    {
        Debug.Log("Failed to initialize the Facebook SDK");
    }
}

private void FacebookLogin()
{
    if (FB.IsLoggedIn)
    {
        OnFacebookLoggedIn();
    }
    else
    {
        var perms = new List<string>(){"public_profile", "email", "user_friends"};
        FB.LogInWithReadPermissions(perms, AuthCallback);
    }
}

private void AuthCallback(ILoginResult result)
{
    if (FB.IsLoggedIn)
    {
        OnFacebookLoggedIn();
    }
    else
    {
        Debug.LogErrorFormat("Error in Facebook login {0}", result.Error);
    }
}

OnFacebookLoggedIn() gets called when the authentication with Facebook is complete and the user's Facebook token is available.

The example below shows how to set up the AuthenticationValues:

C#

private void OnFacebookLoggedIn()
{
    // Facebook's AccessToken class will have session details
    string aToken = AccessToken.CurrentAccessToken.TokenString;
    string facebookId = AccessToken.CurrentAccessToken.UserId;

    var authValues = new AuthenticationValues();
    authValues.AuthType = CustomAuthenticationType.FacebookGaming;  // specifically for gaming apps
    authValues.UserId = facebookId;
    authValues.AddAuthParameter("token", aToken);

    // set the new authValues on the client / runner and connect
    // example for Realtime API

    client.AuthValues = authValues;
    client.ConnectUsingSettings();
}

If your app does not use the Facebook Gaming APIs (described on https://developers.facebook.com/docs/games), the AuthType need to be set to CustomAuthenticationType.Facebook.

From here on, the usual connection workflow happens. This differs a little, depending on the used Photon SDK.

The Realtime API in C# has the IConnectionCallbacks and you want to check OnConnectedToMaster() as well as OnCustomAuthenticationFailed(string debugMessage).

C#

    public void OnConnectedToMaster()
    {
        Debug.Log("Successfully connected to Photon!");
    }

    // something went wrong, check the setup of the Facebook app, login, token, etc.
    public void OnCustomAuthenticationFailed(string debugMessage)
    {
        Debug.LogErrorFormat("Error authenticating to Photon using facebook: {0}", debugMessage);
    }
Back to top