Login with Facebook

Top  Previous  Next

Backendless integrates with Facebook to support user authentication and login into a Backendless application with a Facebook account. Using the integration application developer can provide a way to skip the application registration step and allow users to use their Facebook identity to enter and experience the application. Backendless provides two ways to handle Facebook logins:

Easy Facebook Login - The Facebook SDK is not required on the client. The client application makes a Backendless API call to initiate the login and the user authenticates in a Facebook popup window.
Login with Facebook SDK - The client application uses the Facebook SDK to authenticate the user and then delegates to a Backendless API call to link the Facebook identity to a BackendlessUser instance.

Both approaches - Easy Login and Login with SDK, rely on the concepts and a configuration change described below.

Property Mapping

Backendless user objects may have their own set of user properties. As a result, when a user logs in using their Facebook account, their Facebook Graph API user fields are mapped to the Backendless user properties. A single mapping references a Facebook user field and the name of a Backendless property. The mapping is a required as it dictates which Facebook properties must be used when creating a corresponding Backendless user account. Once the user is authenticated, Backendless obtains the field values from the Facebook user graph object and populates the mapped properties in the BackendlessUser object. For the very first login, Backendless also creates the user in its internal system.

Permissions

The client application can request the permissions from the user to access additional user information or perform various actions. Requested permissions is a collection of string objects passed as an argument in the Backendless API call to login a Facebook user.

The Facebook permissions can be found at https://developers.facebook.com/docs/facebook-login/permissions/v2.2#reference.

Backendless Configuration

Backendless backend must be configured with Facebook's application App ID and secret key. See the "Social Settings" chapter of the user guide (the Manage section) for detailed instructions for configuring Backendless with Facebook.

Easy Facebook Login

Backendless client SDK and the server-side implementation follows the following process to support the easy login option:

1.The client application initiates the Facebook login sequence by calling a method from the Backendless SDK.
2.The invocation results in a UI dialog where the user enters their Facebook credentials and submits the form.
3.The form submission is sent to Facebook.
4.Upon successful login, Facebook executes a redirect to the Backendless servers.
5.Backendless receives the user information from Facebook and returns it back to the client.

The diagram below illustrates the process:

backendless-facebook-login

API

 

Description:

Perform Facebook user login with the default permission. Backendless will obtain the Facebook fields which are available without access token - see the complete list of available fields. Backendless SDK constructs a UI dialog to show the Facebook login screen.

public void Backendless.UserService.loginWithFacebook( 
                                         android.app.Activity context, 
                                         Map<String, String> facebookFieldsMappings, 
                                         AsyncCallback<BackendlessUser> responder )

where

context- must be the current activity context.

facebookFieldMappings - a mapping between the Facebook fields and Backendless user properties. Keys must be the names of the Facebook fields, values - the names of the Backendless properties. See the Property Mapping section for additional details.

responder- an object which receives either a return value or an error from the server. The class must implement the AsyncCallback<BackendlessUser> interface.

Description:

Perform Facebook user login and request the specified permissions. Backendless SDK constructs a UI dialog to show the Facebook login screen.

public void Backendless.UserService.loginWithFacebook( 
                                        android.app.Activity context, 
                                        Map<String, String> facebookFieldsMappings, 
                                        List<String> permissions,
                                        AsyncCallback<BackendlessUser> responder )

where

context- must be the current activity context.

facebookFieldMappings - a mapping between the Facebook fields and Backendless user properties. Keys must be the names of the Facebook fields, values - the names of the Backendless properties. See the Property Mapping section for additional details.

permissions- a collection of the Facebook permissions/scopes which the application requests access to.
responder- an object which receives either a return value or an error from the server. The class must implement the AsyncCallback<BackendlessUser> interface.

Description:

Perform Facebook user login and request the specified permissions. Backendless SDK uses the provided WebView to host the Facebook login screen.

public void Backendless.UserService.loginWithFacebook( 
                                        android.app.Activity context, 
                                        android.webkit.WebView webView,
                                        Map<String, String> facebookFieldsMappings, 
                                        List<String> permissions,
                                        AsyncCallback<BackendlessUser> responder,
                                        boolean stayLoggedIn )

where

context- must be the current activity context. This argument is mandatory.
webView- a view component where the Faceboook login dialog will be rendered. This argument is optional. If not specified, Backendless SDK will construct a UI dialog to show the Facebook login screen.

facebookFieldMappings - a mapping between the Facebook fields and Backendless user properties. Keys must be the names of the Facebook fields, values - the names of the Backendless properties. See the Property Mapping section for additional details. This argument is optional. If not specified, will be defaulted to null and the fields will not be mapped.

permissions- a collection of the Facebook permissions/scopes, which the application requests access to. This argument is optional. If not specified, the default permissions will be requested.
responder- an object, which receives either a return value or an error from the server. The class must implement the AsyncCallback<BackendlessUser> interface. This argument is mandatory.
stayLoggedIn- Boolean value, which should be set to true if a user must remain logged in after leaving an application. This argument is optional.

Example:

Assuming you have Android Activity entitled Main Activity. To enable logging in with loginWithFacebook() method, execute the following steps:

1.Launch the log in flow by using loginWithFacebook() method

Backendless.UserService.loginWithFacebook( LoginActivity.this, new AsyncCallback<BackendlessUser>()
{
  @Override
  public void handleResponse( BackendlessUser loggedInUser )
  {
    // user logged in successfully
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    // failed to log in
  }
} );

2.Alternatively, you can add the mappings and request permissions for logging in:

Map<String, String> facebookFieldMappings = new HashMap<String, String>();
facebookFieldMappings.put( "email", "fb_email" );

List<String> permissions = new ArrayList<String>();
permissions.add( "email" );
Backendless.UserService.loginWithFacebook( this, 
                                           facebookFieldMappings, 
                                           permissions, 
                                           new AsyncCallback<BackendlessUser>()
{
  @Override
  public void handleResponse( BackendlessUser backendlessUser )
  {
    // user logged in successfully
  }

  @Override
  public void handleFault( BackendlessFault backendlessFault )
  {
    // failed to log in
  }
} );


 

Login With Facebook SDK

As the name suggests this option uses the Facebook SDK to handle the login to a Backendless application. The end result is an instance of the BackendlessUser class which contains the property values initialized from the Facebook user account. Using the Facebook SDK with Backendless consists of four steps:

1.Configure the environment with the Facebook SDK. Complete instructions are available at:
https://developers.facebook.com/docs/android/getting-started
2.Configure Backendless application with the Facebook App ID/API Key.
3.Add the CallbackManager field to the activity class (Android Activity)
4.Add onActivityResult() method to the activity class (Android Activity).
5.Use an API call to log in with loginWithFacebookSdk() method as described below.

Adding CallbackManager

Add the CallbackManager field to your Android activity class and initialize it in onCreate as follows. The CallbackManager class is declared in the com.facebook package.

@Override
 public void onCreate( Bundle savedInstanceState )
 {
  // ...
  callbackManager = CallbackManager.Factory.create();
  // ...
 }

Adding onActivityResult() method

Before using API for logging in, you need to add onActivityResult() method to the activity class (Android Activity) of your application as follows:

@Override
 public void onActivityResult( int requestCode, int resultCode, Intent data )
 {
   super.onActivityResult( requestCode, resultCode, data );
   callbackManager.onActivityResult( requestCode, resultCode, data );
 }

If onActivityResult() method is already defined, add the following line to the activity class (Android Activity) of your application:

callbackManager.onActivityResult( requestCode, resultCode, data );

Logging in with loginWithFacebookSdk() method

Logs in a Facebook user into a Backendless application. Uses the SDK to display the login dialog with the specified permissions.

public void Backendless.UserService.loginWithFacebookSdk( 
                                        android.app.Activity context, 
                                        Map<String, String> facebookFieldsMappings, 
                                        List<String> permissions,
                                        com.facebook.CallbackManager callbackManager,
                                        AsyncCallback<BackendlessUser> responder )

where

context- must be the current activity context. This argument is mandatory.

facebookFieldMappings - a mapping between the Facebook fields and Backendless user properties. Keys must be the names of the Facebook fields, values - the names of the Backendless properties. See the Property Mapping section for additional details. This argument is optional. If not specified, will be defaulted to null and the fields will not be mapped.

permissions- a collection of the Facebook permissions/scopes, which the application requests access to. This argument is optional. If not specified, the default permissions will be requested.
callbackManager- manages callbacks into the Facebook SDK from the onActivityResult method in an activity. This argument is mandatory.
responder- an object, which receives either a return value or an error from the server. The class must implement the AsyncCallback<BackendlessUser> interface. This argument is mandatory.

Example:

Assuming you have Android Activity entitled Main Activity. To enable logging in with loginWithFacebookSdk() method, execute the following steps:

1.Add onActivityResult() method to MainActivity class:

@Override
public void onActivityResult( int requestCode, int resultCode, Intent data )
{
  super.onActivityResult( requestCode, resultCode, data );
  callbackManager.onActivityResult( requestCode, resultCode, data );
}

2.Alternatively, if onActivityResult() method is already defined, add the following line to the method body:

callbackManager.onActivityResult( requestCode, resultCode, data );

3.Launch the log in flow by using loginWithFacebookSdk() method

// instead of LoginActivity.this, use the activity class where the API is used
Backendless.UserService.loginWithFacebookSdk( LoginActivity.this, 
                                              callbackManager, 
                                              new AsyncCallback<BackendlessUser>()
{
  @Override
  public void handleResponse( BackendlessUser loggedInUser )
  {
    // user logged in successfully
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    // failed to log in
  }
} );

4.Alternatively, you can add the mappings and request permissions for logging in:

Map<String, String> facebookFieldMappings = new HashMap<String, String>();
facebookFieldMappings.put( "email", "fb_email" );

List<String> permissions = new ArrayList<String>();
permissions.add( "email" );

Backendless.UserService.loginWithFacebookSdk( this, 
                                              facebookFieldMappings, 
                                              permissions, 
                                              callbackManager, 
                                              new AsyncCallback<BackendlessUser>()
{
  @Override
  public void handleResponse( BackendlessUser loggedInUser )
  {
    // user logged in successfully
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    // failed to log in
  }
} );


Please let us know how we can improve the documentation by leaving a comment. All technical questions should be posted to the Backendless Support forum. We do not respond to the technical questions on the documentation pages.: