# Multiple Rules not Triggering and Hubspot Integration not working

**URL:** <https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833>\
**Category:** Get Help\
**Tags:** error, rules, login, roles\
**Created:** [October 20, 2020, 10:47am UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833 "2020-10-20T10:47:54Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason10](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jason10/32/12805_2.png) [@jason10](https://community.auth0.com/u/jason10)\
**Post date:** [October 20, 2020, 10:47am UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833/1 "2020-10-20T10:47:54Z")

</div>

I am combining 3 rules:

1:Assign a default role to a new user on first login ([https://community.auth0.com/t/how-do-i-add-a-default-role-to-a-new-user-on-first-login/25857](https://community.auth0.com/t/how-do-i-add-a-default-role-to-a-new-user-on-first-login/25857))

2: Add New Contact to Hubspot for Marketing (as template given in Rules)

3: Set assigned roles to token: ([Sample Use Cases: Rules with Authorization - Auth0 Docs](https://auth0.com/docs/authorization/sample-use-cases-rules-with-authorization?_ga=2.118904521.1190576816.1603089673-593279948.1601885769#add-user-roles-to-tokens))

If I apply all 3 rules - then on signup I get:

Access Denied and Error Description :Cannot create property ‘app\_metadata’ on string ‘’

If I disable rule 2, then I can signup/login but the rule 3 role is not assigned to the token that is sent to my web app (so I can’t provide role based experiences) - if i logout and log back in in this scenario then rule 3 is triggered and I can provide rule based experiences.

Any idea what’s going wrong?

Note, it does create the user when all 3 are enabled. If after an error, I disable rule 2, log in, then it works. Then if i log back out and re enable rule 2 and log back in, it works fine adding the appropriate data to app\_metadata.

Is there an issue creating app\_metadata on sign up but not log in?  
Also, why would rule 1 and 3 not work together on sign up, but it does on re log in?

Further testing - on signup, rules don’t seem to be able to read `user`?

Further further testing - I believe rule 1 is not playing well with the other rules. Does it not resolve properly within a rule? Does the management function perhaps take not wait for resolving and thus the rule is not performing `callback` before ending the rule?

Does rule 3 not work on signup because it has already sent a token? How would this be resolved?

---

<div class="post-metadata">

**Author:** ![jason10](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jason10/32/12805_2.png) [@jason10](https://community.auth0.com/u/jason10)\
**Post date:** [October 20, 2020, 12:23pm UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833/2 "2020-10-20T12:23:45Z")

</div>

Regarding the Rules only working on reauthentication - there seems to be a solution here:

> [@After applying a Role to a User using a Rule at signup (first login), the permissions are not in the user's access token](https://community.auth0.com/t/after-applying-a-role-to-a-user-using-a-rule-at-signup-first-login-the-permissions-are-not-in-the-users-access-token/32884):
>
> I followed the instructions in [How do I add a default role to a new user on first login?](http://community.auth0.com/t/how-do-i-add-a-default-role-to-a-new-user-on-first-login/25857) to add a Role upon signup/initial login. The Role gets applied correctly. However, the problem is that the initial access token the user is given doesn’t have the permissions for that role in it. If the subsequently login again, the permissions are there. I suppose that this is because either the access token has already been generated by the time the rule is executed or because there’s a slight delay bet…

How would implement this within a rule?

---

<div class="post-metadata">

**Author:** ![jason10](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jason10/32/12805_2.png) [@jason10](https://community.auth0.com/u/jason10)\
**Post date:** [October 21, 2020, 11:50am UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833/3 "2020-10-21T11:50:58Z")

</div>

I use a regular web app in PHP sdk. Maybe there is a way to force to get a new auth token after login()? Below is my login.php code

```
$auth0 = new Auth0([
  'domain' => '',
  'client_id' => '',
  'client_secret' => '',
  'redirect_uri' => '',
  'scope' => 'openid profile email',
]);

$auth0->login();

```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [October 21, 2020, 8:26pm UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833/5 "2020-10-21T20:26:37Z")

</div>

Hi @jason10,

I think we should break this down a bit, seems like there are quite a few things going on here. The 1st and 3rd rule are both about roles, let’s address those first.

Can you post the code to rules 1 and 3? Is it exactly like the ones you posted or have you modified it? Then we can go through and see what you are working with.

Also, do you have a console log in each rule so you know whether or not it is running? Have you tried using the debug console yet?

I think the first and third rule are related, and we could probably combine them to get around some of these issues. Post the code and we will see what we can do.

---

<div class="post-metadata">

**Author:** ![jason10](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jason10/32/12805_2.png) [@jason10](https://community.auth0.com/u/jason10)\
**Post date:** [October 21, 2020, 9:21pm UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833/6 "2020-10-21T21:21:29Z")

</div>

The code is all exactly as in the examples given (except role\_id etc):

I have resolved this currently by turning Rule 1 async as below:

```
async function (user, context, callback) {
  	const ManagementClient = require('auth0@2.27.0').ManagementClient;

    const management = new ManagementClient({
      token: auth0.accessToken,
      domain: auth0.domain
    });
  
    const count = context.stats && context.stats.loginsCount ? context.stats.loginsCount : 0;
    if (count > 1) {
        return callback(null, user, context);
    }

    const params = { id : user.user_id};
    const data = { "roles" : ["my_role_id"]};

    var man = await management.users.assignRoles(params, data);
    callback(null, user, context);
}

```

Thus I do believe that the issue was with the resolve or callback for some reason.

Regarding why the role is not passed to the access token, I believe that the access token is granted pre Rules and thus adding any roles won’t be added to the token until they log out and back in. Thus this is an issue solely with first sign up - as mentioned above, and linked to the other article, I believe I need to refresh the token somehow in php either client side, or it would be good to do so within a rule if possible.

Let me know what you think

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [October 26, 2020, 11:32pm UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833/7 "2020-10-26T23:32:29Z")

</div>

> [@jason10](#):
>
> Regarding why the role is not passed to the access token, I believe that the access token is granted pre Rules and thus adding any roles won’t be added to the token until they log out and back in. Thus this is an issue solely with first sign up - as mentioned above, and linked to the other article, I believe I need to refresh the token somehow in php either client side, or it would be good to do so within a rule if possible.

You are on the right track. This issue is because the `context` object in rules (which contains `context.authorization.roles`) is created before the rule is added.

You can manually add the role to the token in that rule, because you know what the role is and you know this only runs on the first login.

Just add the rule to the token manually in the first login.

---

<div class="post-metadata">

**Author:** ![jason10](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jason10/32/12805_2.png) [@jason10](https://community.auth0.com/u/jason10)\
**Post date:** [October 26, 2020, 11:38pm UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833/8 "2020-10-26T23:38:35Z")

</div>

Thanks for the reply, how would I add the role to the token manually? I assumed I would have done this in rule 3 in the OP.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [October 26, 2020, 11:51pm UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833/9 "2020-10-26T23:51:36Z")

</div>

Are you adding permissions associated to the role or just need the role in the token? If it’s the latter then you can easily add it to every token.

---

<div class="post-metadata">

**Author:** ![jason10](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jason10/32/12805_2.png) [@jason10](https://community.auth0.com/u/jason10)\
**Post date:** [October 26, 2020, 11:57pm UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833/10 "2020-10-26T23:57:08Z")

</div>

the latter for now, I will determine role based ux on the front end

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [October 27, 2020, 12:17am UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833/11 "2020-10-27T00:17:30Z")

</div>

Then you can add the role to every user in the rule you have that adds roles to the token.

---

<div class="post-metadata">

**Author:** ![jason10](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jason10/32/12805_2.png) [@jason10](https://community.auth0.com/u/jason10)\
**Post date:** [October 27, 2020, 10:17am UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833/12 "2020-10-27T10:17:31Z")

</div>

I see ok thanks, so it is not the token that is created pre rules, it is the context that is created pre rules, the token is created after rules. So instead of calling context to set the token, I set it manually on first sign up.

Thanks!

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [October 27, 2020, 4:15pm UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833/13 "2020-10-27T16:15:13Z")

</div>

I think you’ve got it. Let me know if you aren’t able to get it working and I’ll take another look.

---

<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:** [November 11, 2020, 4:15pm UTC](https://community.auth0.com/t/multiple-rules-not-triggering-and-hubspot-integration-not-working/51833/14 "2020-11-11T16:15:36Z")

</div>

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