Trouble hoisting access token from axios.post request

There is nothing in the linked code pen for generating/storing/reusing/obtaining any kind of access token

Authentication is covered here

Since this looks like a server application, where you don’t want the user to login, you probably want to generate an App Access Token, which is what you have already been doing

You should generate then store this somewhere and recall it, IE don’t generate a token for every page load.

Alternatively you can create a server CronJob to fetch, since this application seems to only poll games/top and the streams for those games, so you could create a cronjob to do it, store it in a database, then the front end returns the data from that database. Which then means you are making less calls to Twitch saving your Rate limit, and if 800 people loaded the page at once, the app wouldn’t stop working. IE Implement caching.

The “fix” post I suggested, which you reported as inappropriate is the simplest fix. As it moves the second call to Twitch inside the .then of the call to get a token.

So the second call waits for the result of the first call inside the then. Otherwise both calls were occurring at the same time.

You can either move it to nesting inside .then’s, using Async/await like you have done elsewhere, or move all the calls to Twitch to a CronJob to opimise pageloads/Twitch API interaction

1 Like