fix: resolve authentication race condition with React Query

Previously, API calls were being made before the auth token was configured,
causing initial 401 errors that would retry with 200 after token setup.

Changes:
- Add global auth readiness tracking in apiClient
- Create useAuthReady hook that checks both session and token state
- Update all API hooks to use isAuthReady instead of just session status
- Add AuthWrapper component at layout level for consistent loading UX
- Show spinner while authentication initializes across all pages

This ensures API calls only fire after authentication is fully configured,
eliminating the 401/retry pattern and improving user experience.
This commit is contained in:
2025-08-29 10:18:02 -06:00
parent 59d4c56a48
commit a58a49aeb6
7 changed files with 116 additions and 30 deletions

View File

@@ -19,8 +19,12 @@ export const client = createClient<paths>({
// Create the React Query client wrapper
export const $api = createFetchClient<paths>(client);
// Store the current auth token
// Store the current auth token and ready state
let currentAuthToken: string | null | undefined = null;
let authConfigured = false;
// Export function to check if auth is ready
export const isAuthConfigured = () => authConfigured;
// Set up authentication middleware once
client.use({
@@ -42,6 +46,7 @@ client.use({
// Configure authentication by updating the token
export const configureApiAuth = (token: string | null | undefined) => {
currentAuthToken = token;
authConfigured = true;
};
// Export typed hooks for convenience