# Auth0 does not return user\_metadata

**URL:** <https://community.auth0.com/t/auth0-does-not-return-user-metadata/50114>\
**Category:** Get Help\
**Tags:** rules, login, auth0\
**Created:** [September 21, 2020, 11:22am UTC](https://community.auth0.com/t/auth0-does-not-return-user-metadata/50114 "2020-09-21T11:22:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vicradon](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vicradon/32/21948_2.png) [@vicradon](https://community.auth0.com/u/vicradon)\
**Post date:** [September 21, 2020, 11:22am UTC](https://community.auth0.com/t/auth0-does-not-return-user-metadata/50114/1 "2020-09-21T11:22:26Z")

</div>

I have an auth0 rule that is meant to modify the data returned to the user. It is meant to add a user\_metadata field. I tried doing that with this rule

```auto
async function loginFaunaOnUserLogin(user, context, callback) {
  const faunadb = require("faunadb@2.11.1");

  user.user_metadata = user.user_metadata || {};
  const q = faunadb.query;
  const client = new faunadb.Client({
    secret: "",
  });

  try {
    const user_from_fauna = await client.query(
      q.Map(
        q.Paginate(q.Match(q.Index("users_by_email"), user.email)),
        q.Lambda("ref", q.Get(q.Var("ref")))
      )
    );
   
    if (!user_from_fauna.data[0]) {
      throw new Error("No user with this email exists");
    }

    const role = user_from_fauna.data[0].data.role;
    const collection = `${role[0].toUpperCase()}${role.substr(1)}s`;
    const index = `${role}s_by_email`;

    const user_in_collection = await client.query(
      q.Paginate(
        q.Match(
          q.Index(index),
          user.email
        )
      )
    );
    
    console.log({user_in_collection});
    
    let ref_id;

    if (!user_in_collection.data[0]) {
      const clientDetails = await client.query(
        q.Create(q.Collection(collection), {
          data: {
            metadata: user_from_fauna.data[0].data,
          },
          credentials: {
            password: user.user_id,
          },
        })
      );
      ref_id = clientDetails.ref.id;
    } else {
      ref_id = user_in_collection.data[0].id;
    }
    
    // Login Process
    const credential = await client.query(
      q.Login(q.Ref(q.Collection(String(collection)), ref_id), {
        password: user.user_id,
        ttl: q.TimeAdd(q.Now(), 10, "hour"),
      })
    );  
    
    user.user_metadata= {
      ...user_from_fauna.data[0].data, token: credential.secret, user_id: credential.instance.id
    };

    callback(null, user, context);
  } catch (err) {
    callback(err, user, context);
  }
}

```

But it wasn’t successful. I could only successfully modify the nickname field to add the metadata. Like this

```auto
...
// shortened code    

    // Login Process
    const credential = await client.query(
      q.Login(q.Ref(q.Collection(String(collection)), ref_id), {
        password: user.user_id,
        ttl: q.TimeAdd(q.Now(), 10, "hour"),
      })
    );  
    
    user.nickname = {
      ...user_from_fauna.data[0].data, token: credential.secret, user_id: credential.instance.id
    };

    callback(null, user, context);
  } catch (err) {
    callback(err, user, context);
  }
}

```

This is the schema of what is returned to the user

```auto
email: "",
email_verified: false
name: ""
nickname: {},
picture: "",
sub: ""
updated_at: ""

```

---

<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:** [September 21, 2020, 12:16pm UTC](https://community.auth0.com/t/auth0-does-not-return-user-metadata/50114/2 "2020-09-21T12:16:40Z")

</div>

After having obtained the metadata values from an external source you need to request for them to be saved/persisted to the user profile ([Manage Metadata with Rules](https://auth0.com/docs/rules/metadata#update-user_metadata)). Simply assigning the data to `user_metadata` property in a rule is not sufficient.

Can you check that if you call `auth0.users.updateUserMetadata` method like mentioned in the documentation you can save the data correctly?

---

<div class="post-metadata">

**Author:** ![vicradon](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vicradon/32/21948_2.png) [@vicradon](https://community.auth0.com/u/vicradon)\
**Post date:** [September 21, 2020, 1:39pm UTC](https://community.auth0.com/t/auth0-does-not-return-user-metadata/50114/3 "2020-09-21T13:39:36Z")

</div>

> [@jmangelo](#):
>
> auth0.users.updateUserMetadata

Hello  
Thank you for your reply.

So this is the updated bottom part of the auth rule

```js
   
    user.user_metadata = {
      ...user.user_metadata,
      ...user_from_fauna.data[0].data, token: credential.secret, user_id: credential.instance.id
    };
    auth0.users.updateUserMetadata(user.user_id, user.user_metadata)
    .then(() => {
      callback(null, user, context);
    })
    .catch(function(err){
      callback(err, user, context);
    });

/* callback(null, user, context); */
  } catch (err) {
    callback(err, user, context);
  }
}

```

The user\_metadata property is still not available in the returned object

---

<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:** [September 21, 2020, 2:22pm UTC](https://community.auth0.com/t/auth0-does-not-return-user-metadata/50114/4 "2020-09-21T14:22:13Z")

</div>

Can you go to the users section of the dashboard and access the user profile for the user in question; if in the dashboard you can see the metadata information then the issue you are experiencing is that you are likely expecting to see such information in issued tokens or in responses from the user information endpoint, but by default metadata is not included in tokens issued through OIDC conformant flows.

---

<div class="post-metadata">

**Author:** ![vicradon](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vicradon/32/21948_2.png) [@vicradon](https://community.auth0.com/u/vicradon)\
**Post date:** [September 21, 2020, 2:41pm UTC](https://community.auth0.com/t/auth0-does-not-return-user-metadata/50114/5 "2020-09-21T14:41:47Z")

</div>

> [@jmangelo](#):
>
> metadata is not included in tokens issued through OIDC conformant flows

The user\_metadata of the user is available on the dashboard. Is there any other way I can attach custom data to the returned user object?

---

<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:** [September 21, 2020, 2:46pm UTC](https://community.auth0.com/t/auth0-does-not-return-user-metadata/50114/6 "2020-09-21T14:46:55Z")

</div>

If the data is available in the dashboard the first step would be to confirm in which place (tokens or endpoints) you were expecting the data to be present as well and it is not.

Can you clarify where exactly you would be expecting that information and it is not there? For example, if you need that information in an ID token or in an access token you could consider adding it as custom claims ([Sample Use Cases: Scopes and Claims](https://auth0.com/docs/scopes/sample-use-cases-scopes-and-claims#add-custom-claims-to-a-token)).

---

<div class="post-metadata">

**Author:** ![vicradon](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vicradon/32/21948_2.png) [@vicradon](https://community.auth0.com/u/vicradon)\
**Post date:** [September 21, 2020, 3:30pm UTC](https://community.auth0.com/t/auth0-does-not-return-user-metadata/50114/7 "2020-09-21T15:30:34Z")

</div>

Thank you. I was able to attach the required data to a custom claim.

---

<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:** [October 6, 2020, 3:30pm UTC](https://community.auth0.com/t/auth0-does-not-return-user-metadata/50114/8 "2020-10-06T15:30:52Z")

</div>

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