Twitch Reconcile Lambda
On this page
When a Twitch 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 Twitch 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, userInfo, 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.
userInfo: the response from the Twitch OpenID Connect UserInfo endpointAn OpenID Connect endpoint that returns claims about the authenticated user.. This object is read-only. For a full list of fields, see the Twitch UserInfo reference.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 userInfo object holds claims such as sub, preferred_username, email, email_verified, and picture. FusionAuth uses sub as the unique Id of the Twitch account.
Example Lambda#
The following lambda stores the Twitch account Id, login name, and email verification state in user.data.twitch. The lambda also sets the user's image to the Twitch profile picture on every login.
// stores the Twitch account on the user and keeps the avatar current
function reconcile(user, registration, userInfo, context) {
user.data = user.data || {};
user.data.twitch = {
id: userInfo.sub,
login: userInfo.preferred_username,
emailVerified: userInfo.email_verified === true
};
if (userInfo.picture) {
user.imageUrl = userInfo.picture;
}
}Assigning The Lambda#
After you create the lambda, assign the lambda to the Twitch identity provider.
Navigate to Settings -> Identity Providers . Select your existing Twitch configuration. If you haven't configured Twitch yet, click Add provider and select Twitch.
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.