# Rule for passing app\_metadata (like user's moderator status) to profile payload in React SPA?

**URL:** <https://community.auth0.com/t/rule-for-passing-app-metadata-like-users-moderator-status-to-profile-payload-in-react-spa/45104>\
**Category:** Get Help\
**Created:** [June 23, 2020, 2:47am UTC](https://community.auth0.com/t/rule-for-passing-app-metadata-like-users-moderator-status-to-profile-payload-in-react-spa/45104 "2020-06-23T02:47:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lpatmo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lpatmo/32/22106_2.png) [@lpatmo](https://community.auth0.com/u/lpatmo)\
**Post date:** [June 23, 2020, 2:47am UTC](https://community.auth0.com/t/rule-for-passing-app-metadata-like-users-moderator-status-to-profile-payload-in-react-spa/45104/1 "2020-06-23T02:47:26Z")

</div>

Hi! I’m confused about how to add my app\_metadata object to my profile payload in my React single-page app.

This is what my payload currently looks like:

```auto
name: "luna lovegood"
nickname: "luna"
picture: "https://pbs.twimg.com/profile_images/9151685889/Z1AeI4_b_normal.jpg"
sub: "twitter|1116216"
updated_at: "2020-06-23T02:27:53.940Z"

```

This is what I would like the payload to return:

```auto
name: "luna lovegood"
nickname: "luna"
picture: "https://pbs.twimg.com/profile_images/9151685889/Z1AeI4_b_normal.jpg"
sub: "twitter|1116216"
updated_at: "2020-06-23T02:27:53.940Z",
app_metadata: { moderator: true }

```

In the Auth0 dashboard, I made sure to update the app\_metadata for that particular user to include ` { moderator: true }`. And I read in the docs that I’m supposed to create a rule ([Auth0 Rules - Auth0 Docs](https://auth0.com/docs/rules)).

I took the example rule `Move User Metadata Attributes To Profile Root Attributes` and changed the references to `user_metadata` to `app_metadata`, but couldn’t get it to work.

What am I missing? Is there another easy way to update the `profile` payload with custom user attributes like the users’s moderator status?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![markd](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/markd/32/38919_2.png) [@markd](https://community.auth0.com/u/markd)\
**Post date:** [June 23, 2020, 4:51am UTC](https://community.auth0.com/t/rule-for-passing-app-metadata-like-users-moderator-status-to-profile-payload-in-react-spa/45104/2 "2020-06-23T04:51:16Z")

</div>

Hello @lpatmo,

Welcome to the Community! You need to add the attributes you are interested in to the user’s access token and / or ID token, as demonstrated here:

> **[Node.js API Implementation (Mobile Apps + API)](https://auth0.com/docs/get-started/architecture-scenarios/mobile-api/api-implementation-nodejs)**
>
> The Node.js implementation of the API for the Mobile + API architecture scenario

I have another simple example at the link below, which adds an attribute from `app_metadata` to the ID token:

> <https://github.com/dmark/auth0-cli-utilities/blob/master/rules/add-uuid-to-idtoken.js>

---

<div class="post-metadata">

**Author:** ![lpatmo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lpatmo/32/22106_2.png) [@lpatmo](https://community.auth0.com/u/lpatmo)\
**Post date:** [June 27, 2020, 3:49am UTC](https://community.auth0.com/t/rule-for-passing-app-metadata-like-users-moderator-status-to-profile-payload-in-react-spa/45104/3 "2020-06-27T03:49:42Z")

</div>

Thanks so much for responding, @markd!

I created a rule in auth0:

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

Then I was trying to figure out how to get at the ID token, so I looked at [Auth0.js v9 Reference](https://auth0.com/docs/libraries/auth0js/v9) and changed this bit in my code:

```auto
export function Auth0Provider({ children }) {
         domain: process.env.REACT_APP_DOMAIN,
         client_id: process.env.REACT_APP_clientId,
         redirect_uri: window.location.origin,
+ response_type: "id_token",
       });
       setAuth0Client(auth0);

```

(I am using `import createAuth0Client from "@auth0/auth0-spa-js";`)

But when I restarted the app and went to localhost:8888, I didn’t see the new moderator status in the profile response. Does anyone know what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![markd](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/markd/32/38919_2.png) [@markd](https://community.auth0.com/u/markd)\
**Post date:** [June 27, 2020, 12:11pm UTC](https://community.auth0.com/t/rule-for-passing-app-metadata-like-users-moderator-status-to-profile-payload-in-react-spa/45104/4 "2020-06-27T12:11:16Z")

</div>

Maybe something related an active session? Rules only execute on login events … though even a silent auth0 will trigger rules so that’s probably not the issue. Still, you could call [logout](https://auth0.com/docs/logout/guides/logout-auth0) to make sure your session is terminated.

I’m not certain why it isn’t working, but I wanted to provide some feedback. The lines:

```auto
  user.app_metadata = user.app_metadata || {};
  user.app_metadata.uuid = user.app_metadata.uuid || {};

```

just check for the presence of `app_metadata` and `uuid`. There’s probably a better way to do this in javascript (I’m not a javascript expert), but you would want the second line to be:

```auto
  user.app_metadata.moderator = user.app_metadata.moderator || {};

```

Also the namespace doesn’t need to be the URL of your app. By convention we use a URL, but it does not need to be a resolvable address. For example, we use `https://sso.ourcompany.com/claims`, even though `sso.ourcompany.com` is not a resolvable address.

---

<div class="post-metadata">

**Author:** ![ryanbuckleyca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ryanbuckleyca/32/24594_2.png) [@ryanbuckleyca](https://community.auth0.com/u/ryanbuckleyca)\
**Post date:** [November 10, 2020, 6:29pm UTC](https://community.auth0.com/t/rule-for-passing-app-metadata-like-users-moderator-status-to-profile-payload-in-react-spa/45104/5 "2020-11-10T18:29:15Z")

</div>

I’m having the same issue and wasn’t able to find a solution here or elsewhere:

- My React app is wrapped in the Auth0Provider from @auth0/auth0-react.
- I have an auth0 object in props with keys such as ‘error’, 'getAccessTokenSilently, ‘isAuthenticated’, ‘isLoading’… etc. including my ‘user’ object.
- The user object contains the usual keys (email, name, nickname…etc), but NOT the user\_metadata as I expect after setting a rule to do so.
- The rule is:

> ```
> function (user, context, callback) {
> user.user_metadata = user.user_metadata || {};
> context.idToken[`user_metadata`] = user.user_metadata;
> callback(null, user, context);
> }
> 
> ```

I want to be able to call this.props.auth0.user.user\_metadata from my app as I can with the other key values from the user object that persist.

Thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![ilyanovohatskyi](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ilyanovohatskyi/32/37412_2.png) [@ilyanovohatskyi](https://community.auth0.com/u/ilyanovohatskyi)\
**Post date:** [May 18, 2022, 2:58pm UTC](https://community.auth0.com/t/rule-for-passing-app-metadata-like-users-moderator-status-to-profile-payload-in-react-spa/45104/6 "2022-05-18T14:58:06Z")

</div>

If you’re still having this problem, I’ve found a slightly tricky and not quite correct way to pass data to the useAuth0 hook.

With this rule

```auto
function (user, context, callback) {
  context.user_metadata = user.user_metadata || {};
  callback(null, {...user, profile: user.user_metadata}, context);
}

```

You can use any key from the User class instead of `profile` (except for the custom key).  
 ![image](https://global.discourse-cdn.com/auth0/original/3X/e/0/e03510e9e0a92be2e644cac6de95aa47c70de34f.png)  
I’ve tried different solutions like updating context.idToken. But it didn’t work for me.  
Hope this helps someone.

---

<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:** [January 8, 2024, 8:54am UTC](https://community.auth0.com/t/rule-for-passing-app-metadata-like-users-moderator-status-to-profile-payload-in-react-spa/45104/7 "2024-01-08T08:54:01Z")

</div>

Hey there!

As this topic is related to Rules - Hooks - Actions and Rules & Hooks are being deprecated soon I’m excited to let you know about our next [Ask me Anything session in the Forum on Thursday, January 18 with the Rules, Hooks and Actions team](https://community.auth0.com/t/auth0-community-ask-me-anything-rules-hooks-and-why-actions-matter/124043) on Rules & Hooks and why Actions matter! Submit your questions in the thread above and our esteemed product experts will provide written answers on January 18. Find out more about Rules & Hooks and why Actions matter! Can’t wait to see you there!

[Learn more here!](https://community.auth0.com/t/auth0-community-ask-me-anything-rules-hooks-and-why-actions-matter/124043)
