Authentication on Combu with Facebook

Many users have asked suggestions about how it would be the best way to authenticate on Combu through Facebook, so this is an example of how it can be done in a smooth way.

This post assumes that you have a Unity project and imported the packages of Combu and Facebook SDK (or have Facebook Gameroom as deployment target).

The first thing that we should do to authenticate on Combu after a successful Facebook login is to verify if the Facebook ID is already associated to an account.

Usually the method we want to use in order to integrate Combu with external platforms is User.AuthenticatePlatform, to which we pass a platform key (like “Facebook”, “GameCenter”, etc) and a unique identifier for that platform.

When handling the integration with Facebook, before creating a new account (that is what AuthenticatePlatform does internally if the platform key and ID are not matched) you want to search for a match with the email address of the account, if a direct connection with Facebook ID was not found.

If the Facebook ID is already existing in Combu then we can call directly User.AuthenticatePlatform, else we should verify if there’s an account associated to the email address of Facebook (if the application requested “email” permission and the user accepted):

    1. if an account is associated to the email address then we should ask the user for the password of that account, use the standard Authenticate method (remember that you can pass either a username or an email to Authenticate) and finally use LinkPlatform passing a platform key (like “Facebook”) and the Facebook ID
    2. if no accounts were found associated to the email address (or if email address was not retrieved from Facebook) then we call User.AuthenticatePlatform

So to translate this in code it should be something like this:

/*
 * Assume that "email" is the email address and "facebookID" is the ID of the Facebook authenticated user
 */
if (string.IsNullOrEmpty(email))
{
    // Could not get the email from Facebook login, try to authenticate on Combu by Facebook ID
    AuthenticateWithFacebook(facebookID);
}
else
{
    // An email address was found in the Facebook profile, search for an existing account
    User.Exists(string.Empty, email, (bool userExists, string userError) =>
    {
        if (userExists)
        {
        	/*
        	 * Display a UI to ask the user for the password.
        	 * When the user has entered the password you can call:
        	 * 
        	 * CombuManager.platform.Authenticate(email, password, (bool success, string error) => {
        	 *    if (success) {
        	 *       CombuManager.localUser.LinkPlatform("Facebook", facebookID, (bool linkSuccess, string linkError) => {
        	 *          Debug.Log("Linked to Facebook: " + linkSuccess + " -- " + linkError);
        	 *       });
        	 *    } else {
        	 *       Debug.Log("Authenticate failed: " + error);
        	 *    }
        	 * });
        	 */
        }
        else
        {
        /*
         * No accounts are associated to this email,
         * create a new account for this Facebook ID
         */
            AuthenticateWithFacebook(facebookID);
        }
    });
}

The method AuthenticateWithFacebook could be like this:

void AuthenticateWithFacebook(string facebookID)
{
    CombuManager.localUser.AuthenticatePlatform("Facebook", facebookID, (bool success, string error) => {
    	Debug.Log("Authenticate with Facebook: " + success + " -- " + error);
    });
}

 

You can download a demo scene with sample UI and code that shows the above concept in action at here (import in a project that already have the assets Combu and Facebook).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.