Auth0 Home Blog Docs

React Auth0 possible race condition?


Hi all, we are currently developing a react app with constant fetches to a separate API. Now, we have discovered a possible bug in our software which we are unsure of how to tackle. The issue is that when logging in for the first time, the callback automatically redirects to a portal directory which makes a fetch from the API with access token in the header. When doing it for the first time (i.e. without the token), there is a possible race condition, where the fetch happens before the token is saved in the localStorage, even though the component should render after the handleAuthentication is called. Has anyone seen this yet? What would be the best way to approach fixing it?

Solved already - can be closed.

Solution if anyone had the same issue: turns out the react’s router might be calling code asynchronously - i.e. having handleAuthentication() before return causes the router to call both without waiting for handleAuthentication to finish its job. Fixed it by creating an empty page to redirect to whilst handleAuth finishes and redirects to the actual portal.


Awesome, I’m glad you got it handled. If you run into issues in the future be sure to let us know! Thanks!