fixes fixes fixes tenaancy
This commit is contained in:
@@ -118,6 +118,14 @@ export const useAuthStore = create<AuthState>()(
|
||||
version: 0
|
||||
};
|
||||
localStorage.setItem('auth-storage', JSON.stringify(authState));
|
||||
|
||||
// CRITICAL: Also set tokens as separate items for API interceptor
|
||||
if (newToken) {
|
||||
localStorage.setItem('access_token', newToken);
|
||||
}
|
||||
if (newRefreshToken) {
|
||||
localStorage.setItem('refresh_token', newRefreshToken);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Failed to persist auth state to localStorage:', e);
|
||||
}
|
||||
@@ -229,6 +237,7 @@ export const useAuthStore = create<AuthState>()(
|
||||
const newRefreshToken = tokens.refresh || responseData.refresh || data.refresh || null;
|
||||
|
||||
// CRITICAL: Set auth state AND immediately persist to localStorage
|
||||
// This prevents race conditions where navigation happens before persist
|
||||
set({
|
||||
user: userData,
|
||||
token: newToken,
|
||||
@@ -250,10 +259,20 @@ export const useAuthStore = create<AuthState>()(
|
||||
version: 0
|
||||
};
|
||||
localStorage.setItem('auth-storage', JSON.stringify(authState));
|
||||
|
||||
// CRITICAL: Also set tokens as separate items for API interceptor
|
||||
// This ensures fetchAPI can access tokens immediately
|
||||
if (newToken) {
|
||||
localStorage.setItem('access_token', newToken);
|
||||
}
|
||||
if (newRefreshToken) {
|
||||
localStorage.setItem('refresh_token', newRefreshToken);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Failed to persist auth state to localStorage:', e);
|
||||
}
|
||||
|
||||
// Return user data for success handling
|
||||
return userData;
|
||||
} catch (error: any) {
|
||||
// ALWAYS reset loading on error - critical to prevent stuck state
|
||||
@@ -261,7 +280,6 @@ export const useAuthStore = create<AuthState>()(
|
||||
throw new Error(error.message || 'Registration failed');
|
||||
} finally {
|
||||
// Extra safety: ensure loading is ALWAYS false after register attempt completes
|
||||
// This handles edge cases like network timeouts, browser crashes, etc.
|
||||
const current = get();
|
||||
if (current.loading) {
|
||||
set({ loading: false });
|
||||
|
||||
Reference in New Issue
Block a user