# How do I get a rule to run when a client (not user) logs in?

**URL:** <https://community.auth0.com/t/how-do-i-get-a-rule-to-run-when-a-client-not-user-logs-in/7095>\
**Category:** Get Help\
**Tags:** rules, metadata\
**Created:** [June 15, 2017, 8:10pm UTC](https://community.auth0.com/t/how-do-i-get-a-rule-to-run-when-a-client-not-user-logs-in/7095 "2017-06-15T20:10:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave.hinton](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@dave.hinton](https://community.auth0.com/u/dave.hinton)\
**Post date:** [June 15, 2017, 8:10pm UTC](https://community.auth0.com/t/how-do-i-get-a-rule-to-run-when-a-client-not-user-logs-in/7095/1 "2017-06-15T20:10:25Z")

</div>

What I’m trying to do: include `client_metadata` in the token payload when a client (not a user) logs in.

I’m under the impression that to do this, I have to upload a rule (written in javascript), which will then copy the required fields from `client.client_metadata`to `context.idToken`.

The rule runs when a user logs in, but not when a client logs in. (I know this because the first line of my function calls `console.log()`; I am using the Real-time Webtask Logs extension to view the logging output.)

```
function (client, context, callback) {
  console.log("include Onzo ClientId in token payload");
  var clientId = (client.client_metadata || {}).clientId;
  if ('undefined' !== typeof clientId) {
    console.log("setting clientId", clientId);
    context.idToken"http://onzo.com/clientId"] = parseInt(clientId, 10);
  } else {
    console.log("not setting clientId");
  }
  callback(null, client, context);
}

```

(I’ve been using the `/oauth/token` endpoint to login, with `"grant_type": "client_credentials"`.)

How do I get a rule to run when a client logs in?

(More context: Our customers need to be able to configure their servers to authenticate to our API without human intervention.)

---

<div class="post-metadata">

**Author:** ![prashantT](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/prashantt/32/6002_2.png) [@prashantT](https://community.auth0.com/u/prashantT)\
**Post date:** [June 16, 2017, 6:15am UTC](https://community.auth0.com/t/how-do-i-get-a-rule-to-run-when-a-client-not-user-logs-in/7095/2 "2017-06-16T06:15:45Z")

</div>

I suggest using [Hooks](https://auth0.com/docs/hooks) instead. Hooks support the Client Credentials Exchange extensibility point:

> **[Client Credentials Exchange](https://auth0.com/docs/customize/hooks/extensibility-points/client-credentials-exchange)**
>
> Learn how Hooks can be used with the Client Credentials Exchange extensibility point, which is available for database connections and passwordless connections.

The Hook will also have access to the `client.metadata` object, as outlined in the [starter code](https://auth0.com/docs/hooks/extensibility-points/credentials-exchange#starter-code-and-parameters).

---

<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/how-do-i-get-a-rule-to-run-when-a-client-not-user-logs-in/7095/3 "2018-03-02T05:54:41Z")

</div>


