# Pretty sure I've written the Rule correctly, but am not able to get user\_metadata in the user object in my Gatsby SPA

**URL:** <https://community.auth0.com/t/pretty-sure-ive-written-the-rule-correctly-but-am-not-able-to-get-user-metadata-in-the-user-object-in-my-gatsby-spa/74236>\
**Category:** Get Help\
**Created:** [December 4, 2021, 3:17pm UTC](https://community.auth0.com/t/pretty-sure-ive-written-the-rule-correctly-but-am-not-able-to-get-user-metadata-in-the-user-object-in-my-gatsby-spa/74236 "2021-12-04T15:17:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![treydonovan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/treydonovan/32/33425_2.png) [@treydonovan](https://community.auth0.com/u/treydonovan)\
**Post date:** [December 4, 2021, 3:17pm UTC](https://community.auth0.com/t/pretty-sure-ive-written-the-rule-correctly-but-am-not-able-to-get-user-metadata-in-the-user-object-in-my-gatsby-spa/74236/1 "2021-12-04T15:17:55Z")

</div>

Howdy folks, it’s time for my first ask on here. Relatively new React dev here, and am very keen on Auth0 for my auth component. I followed Sam Julien’s excellent guide on implementation and have got it working, for my SPA which I’ve deployed to Netlify. I am NOT able to parse anything from the user object but user.nickname, and I need one or two user\_metadata for some functions in the app.

For now I just need a single metadata I’ve put into a user profile:

```auto
{
  "chartsite": "lt5"
}

```

I’ve understood that these metadata can be included in the id token without needing a management token by writing a rule which indicates that. So here’s the rule I’ve put into my Auth0 app:

```auto
function (user, context, callback) {
  const namespace = "http://localhost:8000/";
  context.idToken[namespace + 'chartsite'] = user.user_metadata.chartsite;
  return callback(null, user, context);
}

```

Here’s a component snippet which _is_ returning user.nickname but not user.chartsite on login (note, I am working in localhost:8000 and using development keys, apparently still, but that hasn’t hindered deploying the app to a live development site. Nonetheless I am getting warnings about that.

```auto
const Home = ({ user }) => {
  console.log(user.chartsite)
  return (
    <Wrapper2>
      <div class="generic">
        <p>Hi, {user.nickname ? user.nickname : 'friend'}!</p>
        <p>Your site name is {user.chartsite ? user.chartsite : 'unknown'} </p>
      </div>
    </Wrapper2>
  )
}

```

Last thing I’ve been referencing is this doc: [Sample Use Cases: Scopes and Claims](https://auth0.com/docs/scopes/sample-use-cases-scopes-and-claims#add-custom-claims-to-a-token)

Any enlightening assistance most highly appreciated!

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [December 11, 2021, 3:15am UTC](https://community.auth0.com/t/pretty-sure-ive-written-the-rule-correctly-but-am-not-able-to-get-user-metadata-in-the-user-object-in-my-gatsby-spa/74236/3 "2021-12-11T03:15:47Z")

</div>

Hi @treydonovan,

Welcome to the Auth0 Community!

I understand that you’re unable to retrieve the _user\_metadata_ from the [Auth0Client.getProfile() method](https://auth0.github.io/node-auth0/module-auth.AuthenticationClient.html).

First, the _getProfile()_ method calls the [Authentication API get user info endpoint](https://auth0.com/docs/api/authentication#get-user-info), which returns the user profile information from the **access token** obtained from the `/oauth/token` endpoint. The _user\_metadata_ attribute is not one of the attributes returned in the response. Therefore, you’ll need to use the Rule to append custom claims to the tokens.

In this case, you’ll need to update your Rule with something like the following:

```auto
function (user, context, callback) {
 ​const namespace = 'https://myapp.example.com';
 const custom_claim = user.user_metadata.chartsite;

 let idTokenClaims = context.idToken || {};

 idTokenClaims[`${namespace}/chartsite`] = custom_claim;

 context.idToken = idTokenClaims;

 ​return callback(null, user, context);
}

```

Once that is done, you’ll be able to get your custom claims in the access token when using the getProfile() method.

I hoped this helps!

Please don’t hesitate to reach out if you have any further questions.

Thank you.

---

<div class="post-metadata">

**Author:** ![treydonovan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/treydonovan/32/33425_2.png) [@treydonovan](https://community.auth0.com/u/treydonovan)\
**Post date:** [December 11, 2021, 3:35pm UTC](https://community.auth0.com/t/pretty-sure-ive-written-the-rule-correctly-but-am-not-able-to-get-user-metadata-in-the-user-object-in-my-gatsby-spa/74236/4 "2021-12-11T15:35:36Z")

</div>

Hi @rueben.tiow ,  
Thanks a lot for that suggestion.  
I changed the rule, and updated my call from  
user = authResult.idTokenPayload  
to:  
user = authResult.authTokenPayload

Not so simple. I am using the exact code that is published by Auth0 in this thread:

> [@Securing Gatsby with Auth0](https://community.auth0.com/t/securing-gatsby-with-auth0/24389):
>
> warning exclamation We recently release the new [Auth0 React SDK](https://github.com/auth0/auth0-react) that makes it easier to add user authentication to React applications. You can integrate the Auth0 React SDK with Gatsby. Please checkout this [Gatsby sample code](https://github.com/auth0/auth0-react/blob/master/EXAMPLES.md#2-protecting-a-route-in-a-gatsby-app) and [Gatsby example app](https://github.com/auth0/auth0-react/tree/master/examples/gatsby-app) for more details. Thank you for using Auth0 to secure your user experience. In this tutorial, you’ll learn how to set up Auth0 for authentication and identity management on a GatsbyJS site. Brought to you by @sam.julien man_technologist:t2 R…

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [December 14, 2021, 2:17am UTC](https://community.auth0.com/t/pretty-sure-ive-written-the-rule-correctly-but-am-not-able-to-get-user-metadata-in-the-user-object-in-my-gatsby-spa/74236/5 "2021-12-14T02:17:37Z")

</div>

Hi @treydonovan,

Thank you for your reply.

I have made some changes to my initial post and would like to inform you that you should continue appending the custom claims to the **ID Token**.

I found that our [Add user roles to tokens example Rule](https://auth0.com/docs/authorization/authorization-policies/sample-use-cases-rules-with-authorization#add-user-roles-to-tokens) documentation has an example on how to append custom claims to rules.

After testing this myself, I can confirm that I could append custom claims.

Please let me know how this goes for you.

Thank you.

---

<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:** [December 29, 2021, 2:18am UTC](https://community.auth0.com/t/pretty-sure-ive-written-the-rule-correctly-but-am-not-able-to-get-user-metadata-in-the-user-object-in-my-gatsby-spa/74236/6 "2021-12-29T02:18:01Z")

</div>

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