# Add custom claim to access token not working

**URL:** <https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108>\
**Category:** Get Help\
**Created:** [July 10, 2020, 3:44pm UTC](https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108 "2020-07-10T15:44:16Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![katie.at.boxtribute](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/katie.at.boxtribute/32/12789_2.png) [@katie.at.boxtribute](https://community.auth0.com/u/katie.at.boxtribute)\
**Post date:** [July 10, 2020, 3:44pm UTC](https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108/1 "2020-07-10T15:44:16Z")

</div>

Hi-  
I have a react SPA and a flask backend that I’m trying to use with auth0 together. I can authenticate just fine, but I’m having big troubles with authorization… Because of a concurrently-running legacy application using the same databases, I can’t change anything in the db to encompass authorization (like I suspect I’m supposed to to, to use RBAC or something). If I were to just send the access token and use that to get the userinfo, I’d have to do that on every single request from every single user, which doesn’t seem right.

So, what I think should work is either: append a custom claim to my access\_token with the user’s email, or send the id\_token as well to my backend on every request, and be able to extract the user’s email from there.

I have tried both rules and hooks (and am still not super clear about the difference between them, and what would or wouldn’t run when my application requests tokens?) but neither have seemed to actually affect the access token I’m getting back, though both work in the “try it out” feature of the panel where you set them up… And the client exchange hook doesn’t have an obvious way to access the user object anyway, that I can tell.

The contents of my rule are:  
function (user, context, callback) {  
const namespace = ‘[boxtribute.com/](http://boxtribute.com/)’;  
context.accessToken[namespace + ‘email’] = user.email;  
callback(null, user, context);  
}

I also thought about just sending the id\_token as well, but even if I add my client-id to the audience, I can’t decode it with jose.jwt, because  
No access\_token provided to compare against at\_hash claim

Is my best option here REALLY to just send the email address in a separate header field and give up on it being part of any kind of token the backend can consume?

---

<div class="post-metadata">

**Author:** ![kstrongholte](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kstrongholte/32/16354_2.png) [@kstrongholte](https://community.auth0.com/u/kstrongholte)\
**Post date:** [July 10, 2020, 9:18pm UTC](https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108/3 "2020-07-10T21:18:37Z")

</div>

Hi Katie,

Welcome to the Auth0 community!

Namespaces need to start with either `http://` or `https://` as described in our documentation here: [Create Custom Claims](https://auth0.com/docs/tokens/guides/create-namespaced-custom-claims#guidelines)

The contents of your Rule look good to me.

Could you try modifying your namespace and seeing if it solves the issue?

---

<div class="post-metadata">

**Author:** ![katie.at.boxtribute](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/katie.at.boxtribute/32/12789_2.png) [@katie.at.boxtribute](https://community.auth0.com/u/katie.at.boxtribute)\
**Post date:** [July 11, 2020, 5:51pm UTC](https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108/4 "2020-07-11T17:51:16Z")

</div>

it worked!!! Thanks so much. While I have you here though, could you clarify the difference between rules and hooks, and when I should be using each? I looked into some other forum questions and it sounded like rules SHOULDN’T run when the client authenticates, but it seems to in this case. Also, why does the client-exchange hook not have access to the user object? Am I just not understanding the point at which that happens?  
thanks again!

---

<div class="post-metadata">

**Author:** ![rashid779939](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rashid779939/32/27027_2.png) [@rashid779939](https://community.auth0.com/u/rashid779939)\
**Post date:** [July 12, 2020, 6:40pm UTC](https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108/5 "2020-07-12T18:40:24Z")

</div>

As naming convention shows per-registration and post registration, hooks only execute once when new user gets register with Auth0 system  
Rules execute every time user login to the system.

So which is best to use depends upon the specific requirement.

---

<div class="post-metadata">

**Author:** ![noahbrenner](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/noahbrenner/32/12947_2.png) [@noahbrenner](https://community.auth0.com/u/noahbrenner)\
**Post date:** [January 12, 2021, 7:05pm UTC](https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108/6 "2021-01-12T19:05:02Z")

</div>

If starting with `http://` or `https://` is actually required, I recommend updating the documentation, which does _not_ say that. Currently the page you linked to says:

- “Namespaces are arbitrary identifiers, so technically you can call your namespace anything you want.”
- “using the URI of a resource you control is **conventional** ” (emphasis added)

The page _recommends_ using a URI that you control in order to avoid clashes, but nowhere on that page does it say that using a URI is _required_. Confusingly, when I’ve tried out custom rules with non-URI namespaces, their data is included when testing in the dashboard, but it’s not included in the data sent to the client.

---

<div class="post-metadata">

**Author:** ![kstrongholte](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kstrongholte/32/16354_2.png) [@kstrongholte](https://community.auth0.com/u/kstrongholte)\
**Post date:** [January 12, 2021, 10:23pm UTC](https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108/7 "2021-01-12T22:23:48Z")

</div>

Further down the article, it does address the scheme:

> Use the following guidelines:
> 
> - Use any **non-Auth0 HTTP or HTTPS URL** as a namespace identifier. Auth0 domains, which cannot be used as namespace identifiers include:
> - [auth0.com](http://auth0.com)
> - [webtask.io](http://webtask.io)
> - webtask.run
> 
> - Although ideally you will use a URI that you control, the namespace URI does not have to point to an actual resource. It is only used as an identifier; it will not be called.
> - You can use any number of namespaces.

* * *

> Namespaces are arbitrary identifiers, so technically you can **call your namespace** anything you want[1]. However, using the URI of a resource **you control** is conventional[2], following the way XML namespaces are defined.[3]

[1] This refers to how you name your namespace identifier, not how it’s structured. For example, your namespace could be a non-resolving URI (`http://my-api`) or a URL (`http://myapp.com/my-api`).  
[2] I added the emphasis to clarify semantics.  
[3] XML namespaces are [identified by a URI reference](https://www.w3.org/TR/xml-names/#concepts). By definition, URIs [begin with a scheme](https://www.rfc-editor.org/rfc/rfc3986.html#section-3.1).

* * *

> Confusingly, when I’ve tried out custom rules with non-URI namespaces, their data is included when testing in the dashboard, but it’s not included in the data sent to the client.

Namespacing custom claims applies to ID Tokens (in compliance with [OIDC guidance](https://openid.net/specs/openid-connect-core-1_0.html#rfc.section.5.1.2)). IIRC, if you’re testing within the Dashboard (i.e. the “Try This Rule” button), you’re not issuing an ID Token, so namespacing is not enforced.

* * *

I hope this helps clarify things.

---

<div class="post-metadata">

**Author:** ![noahbrenner](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/noahbrenner/32/12947_2.png) [@noahbrenner](https://community.auth0.com/u/noahbrenner)\
**Post date:** [January 12, 2021, 11:59pm UTC](https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108/8 "2021-01-12T23:59:14Z")

</div>

Thanks for the reply! I still don’t think the documentation is clear on this. I don’t have a problem with the way these restrictions work, I just want the documentation to be clear and accurate.

> [@kstrongholte](#):
>
> Use any **non-Auth0 HTTP or HTTPS URL** as a namespace identifier.

- This sentence is introduced by the words “Use the following guidelines:”. I parse this in context as “guidelines to help benefit from our suggestion of using a URI you own in order to avoid collisions”. More to the point, “Guidelines” is not equivalent to “Requirements” so it’s not clear that these “guidelines” _must_ be followed.
- My interpretation of this guideline as optional is supported by the earlier section which says “you can **call your namespace** anything you want”. This “anything” quote refers to “your namespace”, not “your XML namespace”, so it’s not even _implied_ that there’s an enforced limitation on the namespace format with how that sentence is worded.

> [@kstrongholte](#):
>
> XML namespaces are [identified by a URI reference](https://www.w3.org/TR/xml-names/#concepts).

- In the doc, the term “namespace” is used several times before it is _compared_ to “XML namespaces” (which only happens once). The concept of a namespace can mean many different things outside of XML, including simply nesting all data under a single unique key. Also note that we’re modifying JSON objects, not XML documents, so it’s not obvious or intuitive that XML rules apply unless that’s stated explicitly.

> [@kstrongholte](#):
>
> By definition, URIs [begin with a scheme](https://www.rfc-editor.org/rfc/rfc3986.html#section-3.1).

- There are schemes other than `http:` and `https:`, but it seems that no other schemes work with the Auth0 API. If I use a different scheme, the data will not be included (I tried `ftp:`, `git:` and the made up `foo:`). This tells me that the actual rule is _not_ “Must be a URI”, but rather "Must start with the string `"http:"` or `"https:"`. Accuracy matters in technical documents and it would actually be more direct and take less space to simply define this explicit rule (start with http[s]) and then provide details on why the _recommended method_ of following the rule is by using a URI you control in order to avoid collisions.

> [@kstrongholte](#):
>
> if you’re testing within the Dashboard (i.e. the “Try This Rule” button), you’re not issuing an ID Token, so namespacing is not enforced.

- That explanation makes sense, thanks. I’d love to see a toggle there to enable enforcement of the namespacing rules when using the “Try This Rule” button as if an ID token were being generated (bonus points for showing a message if your data was removed due to a namespace rule). Treating the inserted data differently in the dashboard makes these types of issues harder to debug. If there’s a better place to submit a feature request for that, let me know.

I urge you to get the Custom Claims documentation updated to explicitly name the two possible strings that a namespace must start with. These are Auth0’s own rules (not XML namespace rules, even if that was the inspiration), so your documentation needs to explicitly define the rules it’s using.

---

<div class="post-metadata">

**Author:** ![kstrongholte](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kstrongholte/32/16354_2.png) [@kstrongholte](https://community.auth0.com/u/kstrongholte)\
**Post date:** [January 15, 2021, 9:59pm UTC](https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108/9 "2021-01-15T21:59:46Z")

</div>

I agree that the documentation could be clearer.

Would you mind submitting your feedback [here](https://auth0.com/feedback)?

---

<div class="post-metadata">

**Author:** ![rjoshi\_auth0](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rjoshi_auth0/32/33305_2.png) [@rjoshi\_auth0](https://community.auth0.com/u/rjoshi_auth0)\
**Post date:** [April 25, 2022, 8:23pm UTC](https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108/10 "2022-04-25T20:23:52Z")

</div>

I am also in the same boat. I have tried everything but can’t get simple thing in the access token.  
(I am using React SPA)  
My Rule looks like:

```auto
function addEmailToAccessToken(user, context, callback) {
  // This rule adds the authenticated user's email address to the access token.

  var namespace = 'https://dev-xxx.us.auth0.com/';
	console.log("I am here", "at: ", context.accessToken, "it: ", context.idToken);
  if (context.accessToken) {
  	context.accessToken[namespace + 'email'] = user.email;
}
  
 	if (context.idToken) {
  	context.idToken[namespace + 'email'] = user.email;
}
  return callback(null, user, context);
}

```

I can see the rule is fired only when when user logs in (not every time when I call _getAccessTokenSilently_)  
But also when it gets called, the _context.accessToken_ and _context.idToken_ are empty.

```auto
 I am here at: {} it: {}

```

I have also tried using pre made template and even tried _Action/Flows_ Can’t seem to get custom claims/attributes in access token.

Can anyone from Auth0 please help?

---

<div class="post-metadata">

**Author:** ![rjoshi\_auth0](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rjoshi_auth0/32/33305_2.png) [@rjoshi\_auth0](https://community.auth0.com/u/rjoshi_auth0)\
**Post date:** [April 25, 2022, 8:29pm UTC](https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108/11 "2022-04-25T20:29:41Z")

</div>

Got it working. Issue was _ **To be precise, auth0 should not be there in the namespace.** _  
Renaming ‘[https://dev-xxx.us.auth0.com/](https://dev-xxx.us.auth0.com/)’ → ‘[https://somedomain/](https://somedomain/)’ works

---

<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:** [April 26, 2022, 9:51am UTC](https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108/12 "2022-04-26T09:51:06Z")

</div>

Perfect! Thanks for sharing that with the rest of community!

---

<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:** [July 28, 2022, 1:57pm UTC](https://community.auth0.com/t/add-custom-claim-to-access-token-not-working/46108/13 "2022-07-28T13:57:33Z")

</div>


