Setting Invitee's Name During Auth0 Organization Invitation Creation
Last Updated:
Overview
When creating an organization invitation via the Auth0 Management Application Programming Interface (API), setting the invitee's first and last name requires a specific configuration. Auth0 does not provide a native method to populate the root profile fields during invitation creation directly. Pass the name data through user metadata and use a Post-Login Action to transfer the information to the user's root profile upon acceptance.
Applies To
- Auth0
- Organizations
- Management API
- Actions
Solution
How is the invitee's name set during an Auth0 organization invitation?
Pass the name data through user metadata during the invitation creation and map it to the root profile by configuring a Post-Login Action.
- Send a POST request to the Auth0 Management API Create an Organization Invitation endpoint
POST /api/v2/organizations/{id}/invitations. - Include the invitee's first and last name within the
user_metadataobject in the request payload.
Construct the request payload for the Management API that includes the invitee's first and last name in the user_metadata object, using the following JSON example.
NOTE: The following code snippet is provided for example purposes only and should be thoroughly tested before running in a production environment.
{
"inviter": {
"name": "<inviter_name>"
},
"invitee": {
"email": "<invitee@domain.com>"
},
"client_id": "<client_id>",
"user_metadata": {
"given_name": "Jane",
"family_name": "Doe"
},
"send_invitation_email": true
}
Navigate to the Auth0 Dashboard and create a Post-Login Action:
- Configure the Action to read the
user_metadataand transfer the values to the user's root profile fields, such asgiven_nameandfamily_name, using the Management API.
Configure the Post-Login Action to instantiate the Management API client, read the user metadata, and update the root profile fields by implementing the following example code.
NOTE: The following code snippet is provided for example purposes only and should be thoroughly tested before running in a production environment.
const ManagementClient = require('auth0').ManagementClient;
exports.onExecutePostLogin = async (event, api) => {
if (event.user.user_metadata && event.user.user_metadata.given_name) {
const management = new ManagementClient({
domain: event.secrets.domain,
clientId: event.secrets.clientId,
clientSecret: event.secrets.clientSecret,
});
const params = { id: event.user.user_id };
const data = {
given_name: event.user.user_metadata.given_name,
family_name: event.user.user_metadata.family_name
};
try {
await management.users.update(params, data);
} catch (e) {
console.log(e);
}
}
};