How to refresh access / bearer token only when the existing one has expired
I'm using an external API that needs to be authenticated before the actual action can be performed. It works as follows:
1. Â Generate access/bearer token.
2. Call API action which authenticates with the token generated in step #1 before it runs.
Since I'm using Xano as the connector between that third-party service and my frontend, it means that I can technically use the generated access token for a while (about 24hrs) before it expires, at which point I need to generate a new one. If my app has a lot of users utilizing that third-party feature (say 1,000+ in a day) , I'm thinking it might not be efficient to request an access token from the third-party API every single time.Â
Is there a way in Xano I can check if an access token has expired, and then request a new one if it has, otherwise use the latest/existing access token? Does it make a difference in approach if I know how long the access token is supposed to last for (e.g. 1hr, 1 day, etc.)?
PS: I'm in a bit of a chicken-and-egg scenario because the first time I call the third-party service, I need to generate the access token (whose response contains the number of seconds the token is valid for until it expires). So I saved that number of secs, added it to the 'now' timestamp, to get an expiration timestamp. The challenge I have is that if I set up a condition to compare today's date with that expiration date before deciding what to do, it won't work unless the condition is called before the first access token request is called.
Other
2 replies