# Attribute mapping, after Sign in with Apple

**URL:** <https://community.auth0.com/t/attribute-mapping-after-sign-in-with-apple/127388>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [February 15, 2024, 7:33am UTC](https://community.auth0.com/t/attribute-mapping-after-sign-in-with-apple/127388 "2024-02-15T07:33:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sefer](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sefer/32/54849_2.png) [@sefer](https://community.auth0.com/u/sefer)\
**Post date:** [February 15, 2024, 7:33am UTC](https://community.auth0.com/t/attribute-mapping-after-sign-in-with-apple/127388/1 "2024-02-15T07:33:36Z")

</div>

Hi Auth0 Community,

As you know, Auth0 does not provide a convenient way to map user attributes for social logins. So, we need to write a script for Actions to map them.

On the other hand, some of the social IdP providers, such as Apple, give first\_name and last\_name attributes, which are incompatible with OIDC Standard attributes (Standard is given\_name and family\_name).

So I tried to write a script for Pre-User Registration Actions, but the API object does not allow me to change user given\_name and family\_name attributes.

I also tried Login / Post Login Actions, and it’s the same. API object only has setAppMetadata and setUserMetadata, which are not the correct methods for updating the user’s given\_name and family\_name attributes.

At this point, what exactly can I do for this very basic but complicated process?

---

<div class="post-metadata">

**Author:** ![marcelina.barycka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marcelina.barycka/32/56162_2.png) [@marcelina.barycka](https://community.auth0.com/u/marcelina.barycka)\
**Post date:** [February 16, 2024, 3:30pm UTC](https://community.auth0.com/t/attribute-mapping-after-sign-in-with-apple/127388/2 "2024-02-16T15:30:52Z")

</div>

Hi @sefer , welcome!

> [@sefer](#):
>
> On the other hand, some of the social IdP providers, such as Apple, give first\_name and last\_name attributes, which are incompatible with OIDC Standard attributes (Standard is given\_name and family\_name).

You could consider setting Apple connection as a custom social Identity Provider connection to provide fields mapping.  
The place to go would be Auth0 dashboard → Authentication → Social → Create connection → Create Custom.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/6/d/6d539ede5317dd869ae69c14e61142608d8edf33.png)

[This article](https://auth0.com/docs/authenticate/identity-providers/social-identity-providers/oauth2) describes it.  
While setting this up you would provide fields mapping within the Fetch User Profile script.  
The Fetch User Profile script perform a call to the IdP to fetch user profile data.

A sample Fetch User profile script:

 ![image](https://global.discourse-cdn.com/auth0/original/3X/9/4/94dd139f45e3676daf38e89fa54ecdea7268b9d0.png)

If you decide to go with that, you can also find additional support by searching this community for topics like “Fetch user profile script”, “custom social connection”, “Custom IdP” and similar.

I also stay available for follow up questions.

---

<div class="post-metadata">

**Author:** ![sefer](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sefer/32/54849_2.png) [@sefer](https://community.auth0.com/u/sefer)\
**Post date:** [February 16, 2024, 4:35pm UTC](https://community.auth0.com/t/attribute-mapping-after-sign-in-with-apple/127388/3 "2024-02-16T16:35:08Z")

</div>

Here is my solution. Your feedback would be appreciated,

```auto
exports.onExecutePostLogin = async (event, api) => {
  
  // Exchange first and last name with given and family name if the user comes with Apple
  if (event.connection.strategy === 'apple' && event.user.first_name && event.user.last_name) {
    const token = await getManagementApiToken(event.secrets.M2M_CLIENT_ID, event.secrets.M2M_CLIENT_SECRET, event.secrets.AUTH0_AUD, event.secrets.AUTH0_DOMAIN);
    await updateUserProfile(token, event.user.user_id, event.user.first_name, event.user.last_name, event.secrets.AUTH0_DOMAIN);
  }
};

async function getManagementApiToken(clientId, clientSecret, audience, domain) {
  const response = await fetch(`https://${domain}/oauth/token`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      client_id: clientId,
      client_secret: clientSecret,
      audience: audience,
      grant_type: 'client_credentials'
    })
  });

  const data = await response.json();
  return data.access_token;
}

async function updateUserProfile(token, userId, givenName, familyName, domain) {
  await fetch(`https://${domain}/api/v2/users/${userId}`, {
    method: 'PATCH',
    headers: {
      Authorization: `Bearer ${token}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      given_name: givenName,
      family_name: familyName,
      first_name: null,
      last_name: null
    })
  });
}

```

---

<div class="post-metadata">

**Author:** ![marcelina.barycka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marcelina.barycka/32/56162_2.png) [@marcelina.barycka](https://community.auth0.com/u/marcelina.barycka)\
**Post date:** [February 20, 2024, 2:17pm UTC](https://community.auth0.com/t/attribute-mapping-after-sign-in-with-apple/127388/4 "2024-02-20T14:17:38Z")

</div>

Thanks so much for sharing @sefer !

From my side, I would like to also let you know that to interact with the Auth0’s Management API (to update user profile data like `given_name` or `family_name`) you could use within this Action our “[auth0](https://community.auth0.com/t/how-can-i-use-the-management-api-in-actions/64947)” library instead of writing your own function.

A simplified code sample form our Github repo ([it updates user metadata](https://github.com/auth0/node-auth0/blob/master/EXAMPLES.md#update-a-users-user_metadata)):

```auto
exports.onExecutePostLogin = async (event, api) => {
import { ManagementClient } from 'auth0';

const management = new ManagementClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientSecret: '{YOUR_CLIENT_SECRET}',
});

await management.users.update({ id: '{user id}' }, { user_metadata: { foo: 'bar' } });
}

```

I’m sharing it in case you would like to compare performance of calling our Management API with a custom function versus using our library.

[ref](https://github.com/auth0/node-auth0/blob/657c3961/src/management/__generated/models/index.ts#L11892).

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 5, 2024, 2:18pm UTC](https://community.auth0.com/t/attribute-mapping-after-sign-in-with-apple/127388/5 "2024-03-05T14:18:21Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
