Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

How did you resolve the multiple refresh calls issue? Do you use swr hooks on the front end? Been thinking about how to do this myself.



No hooks on the FE side. We use a global lock via a promise. Our API clients are not tied to react in any way.

For all API calls, if the lock is not set, it checks if the JWT is still valid. If it is not, then the lock is set by assigning a new promise to it and saving the resolve call as an external variable to be called after the refresh is done (which resolves the held promise on the other calls, allowing the latest token to be used).

All calls await the lock; it either waits for the refresh to complete or just moves on and performs validation with the currently set token.

Looks like this:

- await on lock; if the lock has been resolved, will just continue on

- Check for JWT validity via exp check (the API server itself would be responsible for checking signature and other validity factors); if not valid, update lock with a new promise and hold the resolver. Perform refresh. Release lock by resolving the promise.

- Use current / refreshed JWT for API call




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: