# M2M token cache solution does not work anymore

**URL:** <https://community.auth0.com/t/m2m-token-cache-solution-does-not-work-anymore/120351>\
**Category:** Get Help\
**Tags:** actions-management-api, management-api\
**Created:** [November 6, 2023, 7:43pm UTC](https://community.auth0.com/t/m2m-token-cache-solution-does-not-work-anymore/120351 "2023-11-06T19:43:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![momo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/momo/32/51266_2.png) [@momo](https://community.auth0.com/u/momo)\
**Post date:** [November 6, 2023, 7:43pm UTC](https://community.auth0.com/t/m2m-token-cache-solution-does-not-work-anymore/120351/1 "2023-11-06T19:43:05Z")

</div>

Hey, there!  
I have been trying to use auth0 actions to get some token caching working since we’ve been hitting the limit last month and I read about it in a thread in here.  
Somehow my solution keeps retrying its execution endlessly when my action is active. I’ve burned throught our token limit in like 10 minutes and now I am at a loss what I’m doing wrong. I have been following this link [here](https://community.auth0.com/t/caching-m2m-access-token-in-actions/100734/4) but it doesn’t seem to work for me.

```auto
// <user>.eu.auth0.com
// source: https://community.auth0.com/t/caching-m2m-access-token-in-actions/100734/4
// auth0@3.7.0
// jwt-decode@4.0.0
//
function loadConfigs(event) {
  if (event.secrets.debug == "1") {
    return {
      domain: event.secrets.domain,
      clientId: event.secrets.client_id,
      clientSecret: event.secrets.client_secret,
      audience: event.secrets.audience,
    };
  } else {
    const requestBody = event.request.body;
    return {
      domain: "<user>.eu.auth0.com",
      clientId: requestBody.client_id,
      clientSecret: requestBody.client_secret,
      audience: requestBody.audience,
    };
  }
}

function getCachedToken(api) {
  try {
    const cachedToken =
      api.cache.get("first").value +
      api.cache.get("second").value +
      api.cache.get("third").value;
    return cachedToken;
  } catch {
    return undefined;
  }
}

function cacheToken(api, token) {
  api.cache.set("first", token.slice(0, 2048));
  api.cache.set("second", token.slice(2048, 4096));
  api.cache.set("third", token.slice(4096));
}

async function handleTokenRequest(event, api) {
  const auth0 = require("auth0");
  const jwt_decode = require("jwt-decode");
  const configs = loadConfigs(event);
  const ManagementClient = auth0.ManagementClient;

  const cachedToken = getCachedToken(api);

  let current_time = Date.now().valueOf() / 1000;
  if (cachedToken != undefined) {
    var decoded = jwt_decode(cachedToken);
  }

  //Initialize management client with existing token to use against Management API
  if (cachedToken != undefined && decoded.exp > current_time) {
    var management = new ManagementClient({
      token: cachedToken,
      domain: configs.domain,
    });
    // return early
    return;
  }

  //Initialize management client with new token to use against Management API
  var management = new ManagementClient({
    domain: configs.domain,
    clientId: configs.clientId,
    clientSecret: configs.clientSecret,
    audience: configs.audience,
    tokenProvider: {
      enableCache: true,
      cacheTTLInSeconds: 86400,
    },
  });

  const newToken = await management.getAccessToken();
  cacheToken(api, newToken);
  management = new ManagementClient({
    token: newToken,
    domain: configs.domain,
  });
}

exports.onExecuteCredentialsExchange = async (event, api) => {
  try {
    await handleTokenRequest(event, api);
  } catch (err) {
    api.access.deny("server_error", err);
  }
};

```

---

<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:** [November 13, 2023, 5:22pm UTC](https://community.auth0.com/t/m2m-token-cache-solution-does-not-work-anymore/120351/3 "2023-11-13T17:22:32Z")

</div>

Hi @momo,

Welcome to the Auth0 Community!

Is there a loop happening somewhere in your code? At first glance, I don’t see anything that would be causing the token call to be executed multiple times and I wouldn’t expect the SDK to retry automatically.

---

<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 28, 2023, 3:19pm UTC](https://community.auth0.com/t/m2m-token-cache-solution-does-not-work-anymore/120351/4 "2023-11-28T15:19:58Z")

</div>

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