This page is a work in progress and could be pending updates.
server | v5 switch to v4  

Photon Facebook Authentication

Contents

Server Side

Facebook App Setup

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

  • Go to Facebook Developers website.
  • 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.

Back To Top

Photon Configuration

  • Open "deploy\NameServer\bin\Photon.NameServer.dll.config".
  • Make sure AuthSettings are enabled, Enabled needs to be true.
  • Optionally set ClientAuthenticationAllowAnonymous to true or false depending on your needs. We recommend setting it to false.
  • Set AuthenticationType to "2" which isthe code for Facebook authentication provider type.
  • Choose any name you want, we used "Facebook" for this example but you can change it.
  • Leave "AuthUrl" empty as it's required but we don't need it as the authentication endpoint is internal.
  • Set AppId and Secret to the values you get from Facebook developers portal for your Facebook app.
  <AuthSettings Enabled="true" ClientAuthenticationAllowAnonymous="false">
    <AuthProviders>
      <AuthProvider Name="Facebook"
                    AuthenticationType="2"
                    AuthUrl=""
                    Secret="Val1"
                    AppId="Val2" />
    </AuthProviders>
  </AuthSettings>

Back To Top

Client Code

The client needs to set the correct authentication type (Facebook, 2) and send a valid Facebook token as a query string parameter named "token".

Back To Top

PUN

Back To Top

Implementation

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

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

To use Facebook Authentication in PUN, add:

private void OnFacebookLoggedIn()
{
    // AccessToken class will have session details
    string aToken = AccessToken.CurrentAccessToken.TokenString;
    string facebookId = AccessToken.CurrentAccessToken.UserId;
    PhotonNetwork.AuthValues = new AuthenticationValues();
    PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Facebook;
    PhotonNetwork.AuthValues.UserId = facebookId; // alternatively set by server
    PhotonNetwork.AuthValues.AddAuthParameter("token", aToken);
    PhotonNetwork.ConnectUsingSettings("1.0");
}

The PUN callbacks for success and error that you can implement are:

public class FacebookAuthTest : MonoBehaviourPunCallbacks
{
    public override void OnConnectedToMaster()
    {
        Debug.Log("Successfully connected to Photon!");
    }

    // something went wrong
    public override void OnCustomAuthenticationFailed(string debugMessage)
    {
        Debug.LogErrorFormat("Error authenticating to Photon using facebook: {0}", debugMessage);
    }
}

To Document Top