Facebook Sign-In - Code

Integrate Facebook Sign-In via the Beamable API [IDEN-Facebook-03]

You can use Facebook Sign-In without the prefab Account Management Flow as well. Below are the integration steps that will allow you to implement Facebook Sign-In & Beamable via Code.

📘

Prerequisites

You will still need to follow some Prerequisites, Configure Unity Project & Configure Beamable steps from the Facebook Sign-In - Guide .

While the guide describes a solid implementation of using the Facebook Sign-In feature, this section will describe several API pieces for you to implement your own wrapper.

Facebook Login & Callbacks

Ultimately you will have to implement the Facebook SDK and listen for the callback with the Login Result. The callback is triggered by the FB.LoginWithReadPermissions() API call.

var perms = new List<string>(){"public_profile", "email"};
FB.LogInWithReadPermissions(perms, result => FB_AuthCallback(result));

In this example, we are forwarding the result into our own method. In this method you should check for errors from Facebook. In addition, you would check to ensure that login was successful.

private void FB_AuthCallback (ILoginResult result) 
{
	if(!string.IsNullOrEmpty(result.Error){
  	 //There was an error to handle
     return;
	}
  
  if(FB.IsLoggedIn){
		//User is logged into facebook successfully.
    
    // AccessToken class will have session details
    var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
    
    return;
	}
}

Beamable Login with AccessToken

📘

Beamable SDK Initialization

The following assumes that you have initialized the Beamable SDK and it is stored in _beamContext variable.

_beamContext = BeamContext.Default; await _beamContext.OnReady;

In the Facebook Sign-In - Guide, we've passed in an additional helper object that will automate the Beamable login flow process. However, in this document we will expose what is "under the hood" of that helper.

Getting the access token string

// AccessToken class will have session details
var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
var tokenString = aToken.TokenString;

You will want a reference to the actual TokenString to pass to the Beamable 3rd Party Login Service.

IsThirdPartyAvailable

With the access token, we need to check if this third party provider is available to the logged in user. It will return a boolean.

var thirdParty = AuthThirdParty.Facebook;
var available = _beamContext.Api.AuthService.IsThirdPartyAvailable(thirdParty, tokenString);

Handle Various Flow Scenarios

Now that we have the Facebook token, we need to account for 3 different scenarios:

New Player
Returning Player already linked to Facebook
Returning Player linking their account with Facebook

We can account for this by determining if we need to:

  • Switch Player - Player wants to switch credentials to a new Player
  • Create New Player - Player wants to Create a new Player account
  • Attach To Current Player - Player wants to Attach this 3rd Party Login to an already authenticated Player.
//Specify the third party auth provider
var thirdParty = AuthThirdParty.Facebook;
//Get information about the user's third party credential
var available = await _beamContext.Api.AuthService.IsThirdPartyAvailable(thirdParty, token);
var userHasCredentials = _beamContext.Api.User.HasThirdPartyAssociation(thirdParty);

//Should we switch to a user that's not currently logged in?
var shouldSwitchUsers = !available;
//Should we create a brand new user with these credentials?
var shouldCreateUser = available && userHasCredentials;
//Should we attach the credentials to an existing user?
var shouldAttachToCurrentUser = available && !userHasCredentials;

Should Switch Users

Here we are authenticating the third party (Facebook) with Beamable by logging them in using LoginThirdParty API.

if(shouldSwitchUsers){
	var op = await _beamContext.Accounts.RecoverAccountWithThirdParty(thirdParty, token);
        op.SwitchToAccount();
}

Should Create User

Here we create a new user, apply the current token to that user, then register the third party login (Facebook) using the AddThirdParty API. The returned result includes the new PlayerAccount.

if(shouldCreateUser){
	var newAccount = await _beamContext.Accounts.CreateNewAccount();
        await newAccount.SwitchToAccount();
        var res = await _beamContext.Accounts.Current.AddThirdParty(thirdParty, token);
        Debug.Log($"Account=[{res.account.GamerTag]");
}

Should Attach To Current User

Here we are attaching to the current user with the Facebook token. This scenario is essentially "auto-login". In this scenario the Facebook player has already been linked with the current Beamable user and we can safely apply the token.

if(shouldAttachToCurrentUser){
     var res = await _beamContext.Accounts.RecoverAccountWithThirdParty(thirdParty, token);
     res.SwitchToAccount();
}

These are all the pieces that you need to successfully implement Facebook Sign-In.