# adminAuth.tokens() getting called multiple times on client load

**URL:** <https://discourse.nodered.org/t/adminauth-tokens-getting-called-multiple-times-on-client-load/62750>\
**Category:** General\
**Created:** [17 May 2022 12:52 UTC](https://discourse.nodered.org/t/adminauth-tokens-getting-called-multiple-times-on-client-load/62750 "2022-05-17T12:52:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dannyhuly](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dannyhuly/32/71604_2.png) [@dannyhuly](https://discourse.nodered.org/u/dannyhuly)\
**Post date:** [17 May 2022 12:52 UTC](https://discourse.nodered.org/t/adminauth-tokens-getting-called-multiple-times-on-client-load/62750/1 "2022-05-17T12:52:56Z")

</div>

Hi,

I have implemented authorization flow using [**adminAuth.tokens()**](https://nodered.org/docs/user-guide/runtime/securing-node-red#custom-authentication-tokens).

I have noticed a strange behavior where the callback will trigger 16 times when the application loads.

**See following code snippet:**

```auto
...
let tokenHits = 0;

...

            adminAuth: {
                tokens: async function(token) {
                    tokenHits++;
                    console.log(`token: ${token} | times: ${tokenHits}`)
                    return { username: 'admin', permissions: '*' };
                }
            },

```

**Request url (In browser):**  
[http://localhost:1880?access\_token=TOKEN](http://localhost:1880?access_token=TOKEN)

**Output:**

```auto
token: TOKEN | times: 1
token: TOKEN | times: 2
token: TOKEN | times: 3
token: TOKEN | times: 4
token: TOKEN | times: 5
token: TOKEN | times: 6
token: TOKEN | times: 7
token: TOKEN | times: 8
token: TOKEN | times: 9
token: TOKEN | times: 10
token: TOKEN | times: 11
token: TOKEN | times: 12
token: TOKEN | times: 13
token: TOKEN | times: 14
token: TOKEN | times: 15
token: TOKEN | times: 16

```

In my real code I send an http request to resolve the user permissions and subsequently sending 16 identical http requests.

Not sure if this is working as intended and I missed something or a bug.

Versions:

- node: v12.16.1
- node-red:2.2.2

Thanks

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [17 May 2022 13:08 UTC](https://discourse.nodered.org/t/adminauth-tokens-getting-called-multiple-times-on-client-load/62750/2 "2022-05-17T13:08:15Z")

</div>

Hi,

The `tokens` function is called for each request that includes a token so that it can be validated and mapped to a user.

When loading the editor it makes multiple calls to the admin api to load the various pieces needed.

It is up to your code to do whatever internal caching of token validation you decide is appropriate.

---

<div class="post-metadata">

**Author:** ![dannyhuly](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dannyhuly/32/71604_2.png) [@dannyhuly](https://discourse.nodered.org/u/dannyhuly)\
**Post date:** [17 May 2022 14:55 UTC](https://discourse.nodered.org/t/adminauth-tokens-getting-called-multiple-times-on-client-load/62750/3 "2022-05-17T14:55:08Z")

</div>

Hi knolleary,  
Thank you for clearing it out.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 July 2022 14:55 UTC](https://discourse.nodered.org/t/adminauth-tokens-getting-called-multiple-times-on-client-load/62750/4 "2022-07-16T14:55:12Z")

</div>

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