Facebook Reconcile Lambda

On this page

When a Facebook identity provider is used to complete a federated login request, FusionAuth will use the configured linking strategy to reconcile the user. FusionAuth will attempt to match the user information returned from the Facebook identity provider to an existing user account or create a new one.

You may optionally utilize a lambda to customize the user and user registration during the authentication event.

When you create a new lambda using the FusionAuth Admin UI, you will be provided an empty function to implement.

Starting in version 1.65.0, you can use attribute mappings to map IdP claims directly to FusionAuth user and registration fields without writing a lambda. Reconcile lambdas and attribute mappings are mutually exclusive. Attempting to configure both on a given IdP results in a validation error.

Lambda Structure#

When you create the lambda with the API, give the function the following signature:

function reconcile(user, registration, facebookUser, context) {
  // Lambda code goes here
}

This lambda must contain a function named reconcile that accepts the following parameters:

  • user: the FusionAuth User object. You can modify this object. However, you can't change the email or username attributes after FusionAuth links the user.
  • registration: the FusionAuth UserRegistration object. You can modify this object.
  • facebookUser: the User returned from the Facebook Me API. This object is read-only. For a full list of fields, see the Graph API User reference.
  • context: Available since 1.64.0 an object containing the context of the request, including access to secrets. This object is read-only.

The User API and Registration API documentation describe the user and registration objects. The fields in facebookUser depend on the user's Facebook settings.

Assigning The Lambda#

After you create the lambda, assign the lambda to the Facebook identity provider.

Navigate to Settings -> Identity Providers . Select your existing Facebook configuration. If you haven't configured Facebook yet, click Add provider and select Facebook.

Default Lambda#

FusionAuth includes a default Facebook reconcile lambda. You can use the default lambda as is or change the lambda:

// This is the default Facebook reconcile, modify this to your liking.
function reconcile(user, registration, facebookUser) {
  // Un-comment this line to see the facebookUser object printed to the event log
  // console.info(JSON.stringify(facebookUser, null, 2));

  user.firstName = facebookUser.first_name;
  user.middleName = facebookUser.middle_name;
  user.lastName = facebookUser.last_name;
  user.fullName = facebookUser.name;

  if (facebookUser.picture && !facebookUser.picture.data.is_silhouette) {
    user.imageUrl = facebookUser.picture.data.url;
  }

  if (facebookUser.birthday) {
    // Convert MM/dd/yyyy -> YYYY-MM-DD
    var parts = facebookUser.birthday.split('/');
    user.birthDate = parts[2] + '-' +  parts[0] + '-' +  parts[1];
  }
}

Example Lambda#

Facebook users control whether Facebook shares the birth year, the month and day, or both. As a result, the birthday field is MM/DD/YYYY, MM/DD, or YYYY. The default lambda expects the full format, and writes an invalid birth date for the other two formats.

The following lambda sets birthDate only from a full birthday. The lambda stores a month and day in user.data.birthday and a year in user.data.birthYear. The lambda also keeps the saved profile values when Facebook leaves a field out.

A lambda that handles full and partial Facebook birthdays.
// fills in the profile and handles the partial birthdays Facebook returns
function reconcile(user, registration, facebookUser, context) {
  user.firstName = facebookUser.first_name || user.firstName;
  user.middleName = facebookUser.middle_name || user.middleName;
  user.lastName = facebookUser.last_name || user.lastName;
  user.fullName = facebookUser.name || user.fullName;

  // skip the gray placeholder image
  if (facebookUser.picture && facebookUser.picture.data && !facebookUser.picture.data.is_silhouette) {
    user.imageUrl = facebookUser.picture.data.url;
  }

  // facebook sends MM/DD/YYYY, MM/DD, or YYYY, depending on what the user shares
  var parts = (facebookUser.birthday || '').split('/');
  user.data = user.data || {};
  if (parts.length === 3) {
    user.birthDate = parts[2] + '-' + parts[0] + '-' + parts[1];
  } else if (parts.length === 2) {
    user.data.birthday = parts[0] + '-' + parts[1];
  } else if (/^\d{4}$/.test(parts[0])) {
    user.data.birthYear = Number(parts[0]);
  }
}