# Passing user option to rule/access-token after login

**URL:** <https://community.auth0.com/t/passing-user-option-to-rule-access-token-after-login/64779>\
**Category:** Get Help\
**Tags:** rules, access-token, auth0\
**Created:** [June 21, 2021, 8:50am UTC](https://community.auth0.com/t/passing-user-option-to-rule-access-token-after-login/64779 "2021-06-21T08:50:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![george.kollias](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/george.kollias/32/27463_2.png) [@george.kollias](https://community.auth0.com/u/george.kollias)\
**Post date:** [June 21, 2021, 8:50am UTC](https://community.auth0.com/t/passing-user-option-to-rule-access-token-after-login/64779/1 "2021-06-21T08:50:17Z")

</div>

Hi all!

I’m using the vanilla JS SPA SDK and I’d like to pass some user-provided info into the access token. Currently, I’m doing sth like:

```auto
options["useroption"] = "useroption-value";
auth0.loginWithRedirect(options);

```

and I extract the option value from the `context.request`’s querystring inside a rule like:

```auto
var useroption = context.request.query.useroption || context.request.body.useroption || "";
context.accessToken["useroption"] = useroption;

```

This case works fine if the option is specified before user’s login (i.e. before `loginWithRedirect`). I was wondering if it’d be possible to pass in custom options to the rule/access-token _after_ a user’s login. I tried that with `getTokenSilently`:

```auto
var options = { useroption: "useroption-value" };
token = await auth0.getTokenSilently(options);

```

but it seems `useroption` doesn’t appear in `context.request`. Any idea if that should work or could be made to work? If no, any other suggestions to achieve the same effect?

Thank you,  
George

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [June 22, 2021, 1:32pm UTC](https://community.auth0.com/t/passing-user-option-to-rule-access-token-after-login/64779/3 "2021-06-22T13:32:10Z")

</div>

Hi @george.kollias,

In order to persist the user options after a user logs in, you’d likely need to save the user options in the user’s metadata in the rule with something like:

```auto
var useroption = context.request.query.useroption || context.request.body.useroption || null;
if (useroption) {
  user.user_metadata.useroption = useroption;
  context.accessToken["useroption"] = useroption;
} else {
  context.accessToken["useroption"] = user.user_metadata.useroption;
}

```

---

<div class="post-metadata">

**Author:** ![george.kollias](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/george.kollias/32/27463_2.png) [@george.kollias](https://community.auth0.com/u/george.kollias)\
**Post date:** [June 22, 2021, 2:05pm UTC](https://community.auth0.com/t/passing-user-option-to-rule-access-token-after-login/64779/4 "2021-06-22T14:05:56Z")

</div>

Thanks @stephanie.chamblee! Just to clarify, I was wondering if it’d be possible the user option to appear in rule’s `context.request` as part of calling `getTokenSilently` (or perhaps another method except `loginWithRedirect`). Any idea if that’d be possible?

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [June 22, 2021, 2:31pm UTC](https://community.auth0.com/t/passing-user-option-to-rule-access-token-after-login/64779/5 "2021-06-22T14:31:56Z")

</div>

Hi @george.kollias,

I just tested this out, and it looks like when you pass custom parameters in the `auth0.getTokenSilently`, they are accessible in the rule in `context.request.query`:

```auto
const getToken = () => {
  try {
    auth0.getTokenSilently({
      useroption: "test123"
    });
  } catch (err) {
    console.log("Log out failed", err);
  }
};

```

This is easier to debug with [Real-time Webtask Logs Extension](https://auth0.com/docs/extensions/real-time-webtask-logs)

Also, it may help to see the API reference for `getTokenSilently`: [https://auth0.github.io/auth0-spa-js/classes/auth0client.html#gettokensilently](https://auth0.github.io/auth0-spa-js/classes/auth0client.html#gettokensilently)

This request will pull the token from memory when a valid one is available, so it is possible that the rule will not run if there is a valid token available.

---

<div class="post-metadata">

**Author:** ![george.kollias](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/george.kollias/32/27463_2.png) [@george.kollias](https://community.auth0.com/u/george.kollias)\
**Post date:** [June 22, 2021, 2:40pm UTC](https://community.auth0.com/t/passing-user-option-to-rule-access-token-after-login/64779/6 "2021-06-22T14:40:18Z")

</div>

I think the problem in my case was the token cache in the SDK. If cache is disabled it seems passing options to `getTokenSilently` works fine. Thank you!

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [June 22, 2021, 2:46pm UTC](https://community.auth0.com/t/passing-user-option-to-rule-access-token-after-login/64779/7 "2021-06-22T14:46:31Z")

</div>

Great, I’m glad you were able to get it working!

---

<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:** [June 28, 2021, 6:27am UTC](https://community.auth0.com/t/passing-user-option-to-rule-access-token-after-login/64779/9 "2021-06-28T06:27:11Z")

</div>

no worries! Here for you!

---

<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:** [June 29, 2021, 10:34am UTC](https://community.auth0.com/t/passing-user-option-to-rule-access-token-after-login/64779/11 "2021-06-29T10:34:03Z")

</div>

Previous message deleted due to SPAM reasons.

---

<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:** [July 14, 2021, 10:34am UTC](https://community.auth0.com/t/passing-user-option-to-rule-access-token-after-login/64779/12 "2021-07-14T10:34:24Z")

</div>

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