# "Invalid code" when enabling rule

**URL:** <https://community.auth0.com/t/invalid-code-when-enabling-rule/70628>\
**Category:** Get Help\
**Tags:** rules, nextjs\
**Created:** [October 1, 2021, 2:58pm UTC](https://community.auth0.com/t/invalid-code-when-enabling-rule/70628 "2021-10-01T14:58:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicolas.riviere](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nicolas.riviere/32/13374_2.png) [@nicolas.riviere](https://community.auth0.com/u/nicolas.riviere)\
**Post date:** [October 1, 2021, 2:58pm UTC](https://community.auth0.com/t/invalid-code-when-enabling-rule/70628/1 "2021-10-01T14:58:50Z")

</div>

Hi,

When I enable this rule in my “staging” tenant, it fail with an “invalid\_grant (Invalid authorization code)” error :

```auto
function(user, context, callback) {
  
  const namespace = 'https://exemple.com';
  
  if (context.authorization && context.authorization.roles) {
    const assignedRoles = context.authorization.roles;

    if (context.idToken) {
      const idTokenClaims = context.idToken;
      idTokenClaims[`${namespace}/roles`] = assignedRoles;
      context.idToken = idTokenClaims;
    }

    if (context.accessToken) {
      const accessTokenClaims = context.accessToken;
      accessTokenClaims[`${namespace}/roles`] = assignedRoles;
      context.accessToken = accessTokenClaims;
    }
  }

  callback(null, user, context);
}

```

When the rule is disabled, authentification work well. I have the same rule on my “development” tenant and it works fine.

I can’t figure why, every parameters are identicals…

Any idea ?

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [October 6, 2021, 1:29pm UTC](https://community.auth0.com/t/invalid-code-when-enabling-rule/70628/2 "2021-10-06T13:29:10Z")

</div>

Hey there!

Hmm, that’s weird I can investigate that if you can send me your tenant name here in the forum using a private message.

---

<div class="post-metadata">

**Author:** ![nicolas.riviere](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nicolas.riviere/32/13374_2.png) [@nicolas.riviere](https://community.auth0.com/u/nicolas.riviere)\
**Post date:** [October 6, 2021, 1:43pm UTC](https://community.auth0.com/t/invalid-code-when-enabling-rule/70628/3 "2021-10-06T13:43:48Z")

</div>

Hi Konrad,

Thanks for your reply,

Finally I managed an other way, by calling my API to fetch the roles, which is not an ideal solution, but it worked.

And, a few time after, i had the exact same problem (“invalid\_grant (Invalid authorization code)”), but with no rules defined… I ended by create different accounts for each environments, and only one tenant by account.

For now it’s working, but something seems wrong, maybe in my configuration…

Thanks

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [October 6, 2021, 2:57pm UTC](https://community.auth0.com/t/invalid-code-when-enabling-rule/70628/4 "2021-10-06T14:57:30Z")

</div>

Gotchya! Thanks for providing that context!

---

<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 21, 2021, 2:58pm UTC](https://community.auth0.com/t/invalid-code-when-enabling-rule/70628/5 "2021-10-21T14:58:03Z")

</div>

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