# Rule to add user email to access token?

**URL:** <https://community.auth0.com/t/rule-to-add-user-email-to-access-token/68248>\
**Category:** Get Help\
**Created:** [August 19, 2021, 2:43pm UTC](https://community.auth0.com/t/rule-to-add-user-email-to-access-token/68248 "2021-08-19T14:43:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![elderscrollsjesus](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/elderscrollsjesus/32/31243_2.png) [@elderscrollsjesus](https://community.auth0.com/u/elderscrollsjesus)\
**Post date:** [August 19, 2021, 2:43pm UTC](https://community.auth0.com/t/rule-to-add-user-email-to-access-token/68248/1 "2021-08-19T14:43:01Z")

</div>

Hi, I found a few posts on this topic already, but I can’t seem to get this to work. I have a custom rule to add user.email to the access token instead of fetching this information from userinfo. The rule looks like this:

```auto
function (user, context, callback) {
    context.accessToken["https://MY_DOMAIN_DOT_COM/claims/email"] = user.email;
    return callback(null, user, context);
}

```

Yet still, I see nothing in the access token. Any ideas?

---

<div class="post-metadata">

**Author:** ![marcus.baker](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marcus.baker/32/8586_2.png) [@marcus.baker](https://community.auth0.com/u/marcus.baker)\
**Post date:** [August 19, 2021, 4:29pm UTC](https://community.auth0.com/t/rule-to-add-user-email-to-access-token/68248/2 "2021-08-19T16:29:43Z")

</div>

Hi,

This works in my tenant, I had to change the custom claim to single quotes.

```auto
function (user, context, callback) {
	context.accessToken['https://MY_DOMAIN_DOT_COM/claims/email'] = user.email;
return callback(null, user, context);
}

```

Including a link to our rules debugging doc in case it is helpful.

> **[Debug Rules](https://auth0.com/docs/customize/rules/debug-rules)**
>
> Learn how to debug your Auth0 rules.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [August 20, 2021, 7:59am UTC](https://community.auth0.com/t/rule-to-add-user-email-to-access-token/68248/3 "2021-08-20T07:59:12Z")

</div>

Thanks for sharing that solution Marcus!

---

<div class="post-metadata">

**Author:** ![Kurtis16](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kurtis16/32/28836_2.png) [@Kurtis16](https://community.auth0.com/u/Kurtis16)\
**Post date:** [August 20, 2021, 11:14am UTC](https://community.auth0.com/t/rule-to-add-user-email-to-access-token/68248/6 "2021-08-20T11:14:15Z")

</div>

> [@marcus.baker](#):
>
> Hi,
> 
> This works in my tenant, I had to change the custom claim to single quotes.
> 
> ```auto
> function (user, context, callback) {
> context.accessToken['https://MY_DOMAIN_DOT_COM/claims/email'] = user.email;
> return callback(null, user, context);
> }
> 
> ```
> 
> Including a link to our rules debugging doc in case it is helpful.  
> [https://auth0.com/docs/rules/debug-rules[mycardstatement](https://www.mycardstatement.pro/)](https://auth0.com/docs/rules/debug-rules%5Bmycardstatement%5D(https://www.mycardstatement.pro/))

Thanks for update and quick reply, i found lots of information here, Really appreciate for help.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [August 20, 2021, 11:54am UTC](https://community.auth0.com/t/rule-to-add-user-email-to-access-token/68248/7 "2021-08-20T11:54:32Z")

</div>

Perfect! Glad to hear that!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [August 24, 2021, 1:28pm UTC](https://community.auth0.com/t/rule-to-add-user-email-to-access-token/68248/9 "2021-08-24T13:28:31Z")

</div>

Deleting previous message due to SPAM reasons.

---

<div class="post-metadata">

**Author:** ![nikolay](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nikolay/32/13374_2.png) [@nikolay](https://community.auth0.com/u/nikolay)\
**Post date:** [September 1, 2021, 10:55am UTC](https://community.auth0.com/t/rule-to-add-user-email-to-access-token/68248/10 "2021-09-01T10:55:44Z")

</div>

For the people who want to migrate their Auth0 Rules, due to the deprecation on **Feb 25, 2022** , here’s how the Auth0 Action version would look like:

```auto
exports.onExecutePostLogin = async (event, api) => {
  // This rule adds the authenticated user's email address to the access token.
  if (event.authorization) {
    const namespace = 'https://MY_DOMAIN_DOT_COM';
    api.accessToken.setCustomClaim(`${namespace}/claims/email`, event.user.email);
  }
};

```

Detailed migration guide can be found here:

> [@Guide: Migrate your custom Rules to Auth0 Actions](https://community.auth0.com/t/guide-migrate-your-custom-rules-to-auth0-actions/68915):
>
> Due to the future deprecation of Auth0 Rules, users are required to migrate their custom Rules to the latest Auth0 Actions. This post will guide you through this migration process. Auth0 Rule to migrate Name: Add user information to access token Script: function (user, context, callback) { // This rule adds the authenticated user's email address to the access token. var namespace = 'https://mydomain.com/'; context.accessToken[namespace + 'email'] = user.email; context.accessToken[…

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [September 1, 2021, 11:43am UTC](https://community.auth0.com/t/rule-to-add-user-email-to-access-token/68248/11 "2021-09-01T11:43:11Z")

</div>

Thanks for sharing it with the rest of community!

---

<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:** [September 16, 2021, 11:43am UTC](https://community.auth0.com/t/rule-to-add-user-email-to-access-token/68248/12 "2021-09-16T11:43:24Z")

</div>

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