Apple Reconcile Lambda
On this page
When an Apple 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 Apple 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, idToken, 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.
idToken: the JSON payload of the validated and decoded Id TokenA JSON Web Token that contains information about the authenticated user. returned from Apple. 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 idToken object holds the payload from Apple. The payload can include registered OpenID Connect claims and custom Apple claims.
Assigning The Lambda#
After you create the lambda, assign the lambda to the Apple identity provider.
Navigate to Settings -> Identity Providers . Select your existing Apple configuration. If you haven't configured Apple yet, click Add provider and select Apple.
Default Lambda#
FusionAuth includes a default Apple reconcile lambda. You can use the default lambda as is or change the lambda:
// This is the default Apple reconcile, modify this to your liking.
function reconcile(user, registration, idToken) {
// Un-comment this line to see the idToken object printed to the event log
// console.info(JSON.stringify(idToken, null, 2));
// During the first login attempt, the user object will be available which may contain first and last name.
if (idToken.user && idToken.user.name) {
user.firstName = idToken.user.name.firstName || user.firstName;
user.lastName = idToken.user.name.lastName || user.lastName;
}
}
Example Lambda#
Apple sends the user's name only during the first login, in the user claim. The following lambda saves the name and builds fullName from the name. On later logins, the lambda keeps the saved name.
When a user chooses Hide My Email, Apple sends a private relay address and sets the is_private_email claim. The lambda stores the flag in user.data.applePrivateEmail, so your application can ask for a direct address later. Apple sends is_private_email as a string or a boolean, and the lambda handles both.
// saves the name Apple sends on the first login and flags Apple private relay addresses
function reconcile(user, registration, idToken, context) {
// apple sends the name only on the first login, so later logins keep the saved name
if (idToken.user && idToken.user.name) {
user.firstName = idToken.user.name.firstName || user.firstName;
user.lastName = idToken.user.name.lastName || user.lastName;
user.fullName = [user.firstName, user.lastName].filter(Boolean).join(' ') || user.fullName;
}
// apple sends this claim as a string or a boolean
user.data = user.data || {};
user.data.applePrivateEmail = String(idToken.is_private_email) === 'true';
}Related
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.
Learn how to map the data from a LinkedIn login to a FusionAuth user and registration with a reconcile lambda.