Twitter Reconcile Lambda
On this page
When a Twitter 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 Twitter 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, twitterUser, 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.
twitterUser: the JSON user object returned by the Twitter Verify Credentials API. This object is read-only.context: Available since1.64.0an 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 twitterUser object holds the profile fields the Twitter API returns.
Assigning The Lambda#
Assign the lambda to the Twitter Identity Provider. Navigate to Settings -> Identity Providers , then edit your Twitter Identity Provider. If you haven't configured Twitter yet, click Add provider and select Twitter.
Default Lambda#
FusionAuth includes a default Twitter reconcile lambda, which you can use or modify:
// This is the default Twitter reconcile, modify this to your liking.
function reconcile(user, registration, twitterUser) {
// Un-comment this line to see the twitterUser object printed to the event log
// console.info(JSON.stringify(twitterUser, null, 2));
// Set name if available in the response
if (twitterUser.name) {
user.fullName = twitterUser.name;
}
// https://developer.twitter.com/en/docs/accounts-and-users/user-profile-images-and-banners.html
if (twitterUser.profile_image_url_https) {
// Remove the _normal suffix to get the original size.
user.imageUrl = twitterUser.profile_image_url_https.replace('_normal.png', '.png');
}
// Set twitter screen_name in registration.
// - This is just for display purposes, this value cannot be used to uniquely identify
// the user in FusionAuth.
registration.username = twitterUser.screen_name;
}
Example Lambda#
The default lambda removes the _normal suffix only from PNG profile images. The following lambda removes the suffix from any image type. The lambda skips the placeholder image Twitter shows for users with no profile image. The lambda also stores the user's location in user.data.location when the Twitter profile has a location.
// fills in the profile, the full-size profile image, and the location from Twitter
function reconcile(user, registration, twitterUser, context) {
user.fullName = twitterUser.name || user.fullName;
// remove the _normal suffix from any image type to get the original size, and skip the placeholder image
if (twitterUser.profile_image_url_https && !twitterUser.default_profile_image) {
user.imageUrl = twitterUser.profile_image_url_https.replace(/_normal(\.\w+)$/, '$1');
}
// users can change the screen name, so use the value for display only
registration.username = twitterUser.screen_name;
if (twitterUser.location) {
user.data = user.data || {};
user.data.location = twitterUser.location;
}
}Related
Learn how to map the data from an Apple login to a FusionAuth user and registration with a reconcile lambda.
Learn how to map the data from an Epic Games login to a FusionAuth user and registration with a reconcile lambda.
Learn how to map the data from a Facebook login to a FusionAuth user and registration with a reconcile lambda.
Learn how to map the data from a Google login to a FusionAuth user and registration with a reconcile lambda.