Skip to content

Login with Facebook

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 register with and experience the application.

In order to integrate Facebook login with Backendless, client application must use the Facebook SDK to authenticate the user and then makes a Backendless API call to link the Facebook identity to a BackendlessUser instance.

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 via "property mapping". There may be multiple mapping, each must reference  a Facebook user field and a name of a Backendless property. Once a 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.

Backendless Configuration

A Backendless application (the server side) 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.

Integration API

The API provides a way to "exchange" Facebook access token for Backendless user object which represents the corresponding Facebook user. Before using the API, application must perform login to Facebook using its SDK. The integration process consists of the following steps:

When creating an application with uses the Facebook SDK, make sure to follow the steps below:

  1. Add Facebook SDK to your project per the instructions from:
    https://developers.facebook.com/docs/facebook-login/ios/
  2. Add all the required code to your app which performs login to Facebook and obtains FBSDKAccessToken. The token is all that is required by Backendless in order to login a Facebook user.
  3. Once the app obtains the Facebook access token, use the following Backendless API:
- (void)logingWithFacebookWithAccessToken:(NSString * _Nonnull)accessToken fieldsMapping:(NSDictionary<NSString *,NSString *> * _Nonnull)fieldsMapping responseHandler:^(BackendlessUser * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func loginWithFacebook(accessToken: String, fieldsMapping: [String: String], responseHandler: ((BackendlessUser) -> Void)!, errorHandler: ((Fault) -> Void)!)

where:

Argument                Description
accessToken represents an immutable access token for using Facebook services.
fieldsMapping a mapping between the facebook fields and Backendless user properties. Keys must be the names of the Facebook fields, values - the names of the properties which will be created in the Users table in Backendless. See the Property Mapping section for additional details.

Example

- (void)loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error {
    if (error) {
        NSLog(@"Facebook login failed: %@", error.description);
    }
    else {
        FBSDKAccessToken *accessToken = [FBSDKAccessToken currentAccessToken];
        if (accessToken) {
            [Backendless.shared.userService logingWithFacebookWithAccessToken:accessToken.tokenString fieldsMapping:@{@"email": @"email"} responseHandler:^(BackendlessUser *loggedUser) {
                [[[UIAlertView alloc] initWithTitle:@"Login succeed" message:@"You are now logged in" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
            } errorHandler:^(Fault *fault) {
                [[[UIAlertView alloc] initWithTitle:@"Facebook login failed" message:fault.message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
            }];
        }
    }
}
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
    if error != nil {
        print("Facebook login failed: \(error?.localizedDescription)")
    }
    else {
        if let accessToken = AccessToken.current {
            Backendless.shared.userService.logingWithFacebook(accessToken: accessToken.tokenString, fieldsMapping: ["email": "email"], responseHandler: { loggedUser in
                print("User has been logged in with Facebook")
            }, errorHandler: { fault in
                print("Error: \(fault.message ?? "")")
            })
        }
    }
}