This commit is contained in:
IGNY8 VPS (Salman)
2025-12-08 14:57:36 +00:00
parent 144e955b92
commit c09c6cf7eb
5 changed files with 201 additions and 86 deletions

View File

@@ -92,13 +92,35 @@ export const useAuthStore = create<AuthState>()(
throw createAuthError('Active subscription required. Visit igny8.com/pricing to subscribe.', 'PLAN_REQUIRED');
}
const newToken = responseData.access || tokens.access || data.access || null;
const newRefreshToken = responseData.refresh || tokens.refresh || data.refresh || null;
// CRITICAL: Set auth state AND immediately persist to localStorage
// This prevents race conditions where API calls happen before persist middleware writes
set({
user: userData,
token: responseData.access || tokens.access || data.access || null,
refreshToken: responseData.refresh || tokens.refresh || data.refresh || null,
token: newToken,
refreshToken: newRefreshToken,
isAuthenticated: true,
loading: false
});
// Force immediate persist to localStorage (don't wait for Zustand middleware)
try {
const authState = {
state: {
user: userData,
token: newToken,
refreshToken: newRefreshToken,
isAuthenticated: true,
loading: false
},
version: 0
};
localStorage.setItem('auth-storage', JSON.stringify(authState));
} catch (e) {
console.warn('Failed to persist auth state to localStorage:', e);
}
} catch (error: any) {
// ALWAYS reset loading on error - critical to prevent stuck state
set({ loading: false });
@@ -125,12 +147,49 @@ export const useAuthStore = create<AuthState>()(
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;domain=" + window.location.hostname;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;domain=." + window.location.hostname;
}
// Clear all localStorage to prevent state contamination
localStorage.clear();
// Clear sessionStorage as well
// IMPORTANT: Selectively clear auth-related localStorage items
// DO NOT use localStorage.clear() as it breaks Zustand persist middleware
const authKeys = ['auth-storage', 'auth-store', 'site-storage', 'sector-storage', 'billing-storage'];
authKeys.forEach(key => {
try {
localStorage.removeItem(key);
} catch (e) {
console.warn(`Failed to remove ${key}:`, e);
}
});
// Clear sessionStorage
sessionStorage.clear();
// Reset auth state
set({ user: null, token: null, refreshToken: null, isAuthenticated: false, loading: false });
// Reset auth state to initial values
set({
user: null,
token: null,
refreshToken: null,
isAuthenticated: false,
loading: false
});
// Reset other stores that depend on auth
try {
// Dynamically import and reset site store
import('./siteStore').then(({ useSiteStore }) => {
useSiteStore.setState({ activeSite: null, loading: false, error: null });
});
// Dynamically import and reset sector store
import('./sectorStore').then(({ useSectorStore }) => {
useSectorStore.setState({ activeSector: null, sectors: [], loading: false, error: null });
});
// Dynamically import and reset billing store
import('./billingStore').then(({ useBillingStore }) => {
useBillingStore.setState({ balance: null, loading: false, error: null });
});
} catch (e) {
console.warn('Failed to reset stores on logout:', e);
}
},
register: async (registerData) => {
@@ -166,13 +225,35 @@ export const useAuthStore = create<AuthState>()(
const tokens = responseData.tokens || {};
const userData = responseData.user || data.user;
const newToken = tokens.access || responseData.access || data.access || null;
const newRefreshToken = tokens.refresh || responseData.refresh || data.refresh || null;
// CRITICAL: Set auth state AND immediately persist to localStorage
set({
user: userData,
token: tokens.access || responseData.access || data.access || null,
refreshToken: tokens.refresh || responseData.refresh || data.refresh || null,
token: newToken,
refreshToken: newRefreshToken,
isAuthenticated: true,
loading: false
});
// Force immediate persist to localStorage (don't wait for Zustand middleware)
try {
const authState = {
state: {
user: userData,
token: newToken,
refreshToken: newRefreshToken,
isAuthenticated: true,
loading: false
},
version: 0
};
localStorage.setItem('auth-storage', JSON.stringify(authState));
} catch (e) {
console.warn('Failed to persist auth state to localStorage:', e);
}
return userData;
} catch (error: any) {
// ALWAYS reset loading on error - critical to prevent stuck state