# Adding user email to the ID Token using Actions?

**URL:** <https://community.auth0.com/t/adding-user-email-to-the-id-token-using-actions/92484>\
**Category:** Get Help\
**Tags:** auth0\
**Created:** [October 16, 2022, 4:43pm UTC](https://community.auth0.com/t/adding-user-email-to-the-id-token-using-actions/92484 "2022-10-16T16:43:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthias.lee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/matthias.lee/32/40629_2.png) [@matthias.lee](https://community.auth0.com/u/matthias.lee)\
**Post date:** [October 16, 2022, 4:43pm UTC](https://community.auth0.com/t/adding-user-email-to-the-id-token-using-actions/92484/1 "2022-10-16T16:43:13Z")

</div>

I’m having a hard time adding user emails to the id-token.

Based on [this thread](https://community.auth0.com/t/how-to-add-roles-and-permissions-to-the-id-token-using-actions/84506) and [this thread](https://community.auth0.com/t/i-cant-add-the-user-email-on-access-token/83958) it should be simple, but for me **most** fields seem to be **undefined** on the user object.

Currently i have this:

```auto
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://<MY URL>';
  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/claims/email`, event.user.email);
    console.log(event.user);
  }
}

```

Monitoring-\>Logs confirms that the fields are undefined:

```auto
<snip> 
 email_verified: false,\n email: undefined,\n family_name: undefined,\n given_name: undefined,\n identities: [\n {\n connection: 'EXTERNAL-AD',\n isSocial: false,\n provider:",
<snip>

```

**To note:** these users are coming in through an Azure Active Directory, so I also checked `user.upn` as just in case the mapping to `email` wasnt working, but that was also undefined.

Any ideas how I could get at that email?

In related news, I had no problems attaching the roles in exactly the same manner as above.

---

<div class="post-metadata">

**Author:** ![matthias.lee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/matthias.lee/32/40629_2.png) [@matthias.lee](https://community.auth0.com/u/matthias.lee)\
**Post date:** [October 16, 2022, 5:24pm UTC](https://community.auth0.com/t/adding-user-email-to-the-id-token-using-actions/92484/2 "2022-10-16T17:24:24Z")

</div>

I was able to find a workaround using “rules” by doing this:

```auto
context.idToken[namespace + 'email'] = user.upn;

```

it’s not great, but will do for the moment.

---

<div class="post-metadata">

**Author:** ![thameera](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/thameera/32/13430_2.png) [@thameera](https://community.auth0.com/u/thameera)\
**Post date:** [October 17, 2022, 5:12am UTC](https://community.auth0.com/t/adding-user-email-to-the-id-token-using-actions/92484/3 "2022-10-17T05:12:31Z")

</div>

The ID token will get an `email` claim automatically if the following is true:

1. The user object has an `email` attribute
2. You request the `email` scope in the `/authorize` request

If either of these are false, you can still add it by using a rule or an Action. You should be able to do that without using a namespace - eg: `context.idToken.email`.

---

<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:** [October 31, 2022, 5:12am UTC](https://community.auth0.com/t/adding-user-email-to-the-id-token-using-actions/92484/4 "2022-10-31T05:12:57Z")

</div>

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