# User app metadata set in rule is only available in JWT in the next login

**URL:** <https://community.auth0.com/t/user-app-metadata-set-in-rule-is-only-available-in-jwt-in-the-next-login/7154>\
**Category:** Get Help\
**Tags:** app\_metadata, jwt, bug\
**Created:** [June 20, 2017, 7:07pm UTC](https://community.auth0.com/t/user-app-metadata-set-in-rule-is-only-available-in-jwt-in-the-next-login/7154 "2017-06-20T19:07:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Meister](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/david_meister/32/6034_2.png) [@David\_Meister](https://community.auth0.com/u/David_Meister)\
**Post date:** [June 20, 2017, 7:07pm UTC](https://community.auth0.com/t/user-app-metadata-set-in-rule-is-only-available-in-jwt-in-the-next-login/7154/1 "2017-06-20T19:07:56Z")

</div>

I’m using auth0.js v8.

### Steps to reproduce

#### Step 1

Call `authorize()` with `id_token` as `requestType` to get a JWT and `scope` `openid color`.

#### Step 2

Setup a rule that modifies `user.app_metadata`

```
function (user, context, callback) {
  user.app_metadata = user.app_metadata || {};
  user.app_metadata.color = user.app_metadata.color || 'blue';

  auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
    .then(function(){
        callback(null, user, context);
    })
    .catch(function(err){
        callback(err);
    });
}

```

This is simply the example rule `Add persistent attributes to the user` with `user.app_metadata` swapped out with `user.user_metadata`.

#### Step 3

Login with any user and console log the JWT

You will see that `color` is _NOT_ set in the JWT.

You will also see in the Auth0 backend that `color` is set on the user.

#### Step 4

Logout

#### Step 5

Login again with that same user and console log the JWT

You will see that `color` is now set in the JWT.

You will also see that in the Auth0 backend that `color` is still set on the user.

### Expected behaviour

I expect that the JWT tokens received in step 3 and step 5 are identical.

---

<div class="post-metadata">

**Author:** ![jmangelo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jmangelo/32/7923_2.png) [@jmangelo](https://community.auth0.com/u/jmangelo)\
**Post date:** [June 21, 2017, 10:05pm UTC](https://community.auth0.com/t/user-app-metadata-set-in-rule-is-only-available-in-jwt-in-the-next-login/7154/2 "2017-06-21T22:05:10Z")

</div>

The automatic inclusion of custom information in the ID token (non-standard OpenID Connect claims for which `color` would be a good example) is part of what’s now considered the legacy authentications flows; these are still supported, but it’s not likely that the behavior you experienced will change, in particular, because there’s also a workaround. As a side note, in an OIDC compliant flow you would have to explicitly [add custom claims](https://auth0.com/docs/api-auth/tutorials/adoption/scope-custom-claims#custom-claims) to the token so this would end-up being a non-issue.

**If you want that the new `app_metadata` property being added to also be considered when issuing the ID token for the current transaction** then you can update your rule logic to something like the following:

```
function (user, context, callback) {
  user.app_metadata = user.app_metadata || {};

  if (!user.app_metadata.color) {
    var color = "blue";

    user.app_metadata.color = color;

    // set it also at the user level so that is available to be issued
    // in the token for this transaction
    user.color = color;

    auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
      .then(function() { callback(null, user, context); })
      .catch(callback);
  } else {
    callback(null, user, context);
  }
}

```

The above should get you consistent behavior for the scenario you described.

---

<div class="post-metadata">

**Author:** ![David\_Meister](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/david_meister/32/6034_2.png) [@David\_Meister](https://community.auth0.com/u/David_Meister)\
**Post date:** [June 23, 2017, 7:26am UTC](https://community.auth0.com/t/user-app-metadata-set-in-rule-is-only-available-in-jwt-in-the-next-login/7154/3 "2017-06-23T07:26:35Z")

</div>

OK yes, it seems pushing the data onto the user object itself seems to workaround this inconsistency.

Hopefully this behaviour continues to be supported 🙂

---

<div class="post-metadata">

**Author:** ![David\_Meister](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/david_meister/32/6034_2.png) [@David\_Meister](https://community.auth0.com/u/David_Meister)\
**Post date:** [June 23, 2017, 7:26am UTC](https://community.auth0.com/t/user-app-metadata-set-in-rule-is-only-available-in-jwt-in-the-next-login/7154/4 "2017-06-23T07:26:35Z")

</div>

OK yes, it seems pushing the data onto the user object itself seems to workaround this inconsistency.

Hopefully this behaviour continues to be supported 🙂

---

<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:** [March 2, 2018, 5:54am UTC](https://community.auth0.com/t/user-app-metadata-set-in-rule-is-only-available-in-jwt-in-the-next-login/7154/5 "2018-03-02T05:54:47Z")

</div>


