Hi everyone, I'm new to TanStack Query (v5) and currently integrating it into a React frontend that uses MSAL.js for authentication with Azure AD.
After users successfully log in via MSAL, I need to call a POST /users/provision endpoint to create or update them in the backend. This provisioning step fetches role information, ensures the user is stored in the DB, and returns their data. Only after that should the rest of my app start loading anything else.
The issue is that React Query hooks like useCurrentUser, useSmsCredits, useSmsLogs etc. run immediately and make requests before provisioning is complete. This causes race conditions and unauthorized errors, especially when it's the user’s first login.
What I want is this: when a user logs in, the frontend should call /users/provision once and only after that allow the rest of the React Query system to run. If the user reloads the page while still logged in (session is kept in localStorage), only /me should be called to hydrate the user, no need to call provision again. But if they explicitly log out and then log back in, provision should run again to sync roles from Azure.