# Manage User Metadata and App Metadata

**URL:** <https://community.auth0.com/t/manage-user-metadata-and-app-metadata/123323>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [December 19, 2023, 12:46am UTC](https://community.auth0.com/t/manage-user-metadata-and-app-metadata/123323 "2023-12-19T00:46:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![walter.adbe](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/walter.adbe/32/33268_2.png) [@walter.adbe](https://community.auth0.com/u/walter.adbe)\
**Post date:** [December 19, 2023, 12:46am UTC](https://community.auth0.com/t/manage-user-metadata-and-app-metadata/123323/1 "2023-12-19T00:46:56Z")

</div>

Hi,

I have a issue with Auth0 user management.  
Following is the user case.  
I have three application is Auth0

1. Management API
2. Machine to Machine application
3. Single page application.

I also have two database connection

1. Database A
2. Database B  
Note: Both the database connection in Auth0 are connected to a common database is AWS.

I am also making use of App\_metadata and User\_Metadata to build tokens.

I am observing the following behaviour.  
When i login using Single page application [user@gmail.com], I am adding some information in App\_metadata and User\_metadata. Single page application is connected to Database A

When i login using Machine to Machine application [I have my own login logic in the backend and i am using Auth0 to authenticate], some information is added to User and App metadata.

The problem i face is in User management.  
Application 1: When i login using SPA a user is added in User Management → Users [with [user@gmail.com](mailto:user@gmail.com), Connection: Database A].  
Application 2: When i login using M to M a user is added in User Management → Users [with [user@gmail.com](mailto:user@gmail.com), Connection: Database B].  
But when i login using Application 2, Application 1 App and User metadata is also getting updated which in-turn causes issue when new access token is issued.  
Application 1 is creating a new access token using App and User metadata which was recently updated.  
Will it be possible to make sure Application 1 and Application 2 [App and User metadata] remains distinct and only updated respective one.

---

<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 19, 2023, 4:30pm UTC](https://community.auth0.com/t/manage-user-metadata-and-app-metadata/123323/3 "2023-12-19T16:30:21Z")

</div>

Hi @walter.adbe,

Thanks for reaching out to the Auth0 Community!

> [@walter.adbe](#):
>
> But when i login using Application 2, Application 1 App and User metadata is also getting updated which in-turn causes issue when new access token is issued.

It appears that the way you have written your Post-Login Action script to append user\_metadata and app\_metadata properties to the access token, does not consider the condition where the user is authenticating against the SPA or M2M.

Consider using a conditional statement to check which app the user is authenticating against when appending custom claims. For example:

```auto
exports.onExecutePostLogin = async (event, api) => {
  const namespace = "http://yourNamespace/";
  if(event.authorization){
    if(event.client.name === 'SPA'){
      api.accessToken.setCustomClaim(`${namespace}user_metadata`, event.user.user_metadata)
      api.accessToken.setCustomClaim(`${namespace}app_metadata`, event.user.app_metadata)
    } else if (event.client.name === 'M2M'){
      api.accessToken.setCustomClaim(`${namespace}user_metadata`, event.user.user_metadata)
      api.accessToken.setCustomClaim(`${namespace}app_metadata`, event.user.app_metadata)
    }
  }
};

```

Doing it this way will only append custom claims based on the application the user logs into.

Please let me know if you have any questions.

Thanks,  
Rueben

---

<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:** [January 2, 2024, 4:31pm UTC](https://community.auth0.com/t/manage-user-metadata-and-app-metadata/123323/4 "2024-01-02T16:31:20Z")

</div>

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