logout issues # 2
This commit is contained in:
@@ -83,101 +83,7 @@ const LayoutContent: React.FC = () => {
|
||||
checkTokenAndLoad();
|
||||
}, [isAuthenticated]); // Run when authentication state changes
|
||||
|
||||
// Sector loading moved to PageHeader component
|
||||
// This ensures sectors are only loaded when content pages (Planner/Writer/Optimizer) mount
|
||||
// Account/billing pages don't use PageHeader with site/sector selector, so they won't trigger sector loading
|
||||
// This prevents unnecessary 404 errors on /account/plans and similar routes
|
||||
|
||||
// Refresh user data on mount and when app version changes (after code updates)
|
||||
// This ensures changes are reflected immediately without requiring re-login
|
||||
useEffect(() => {
|
||||
if (!isAuthenticated) return;
|
||||
|
||||
const APP_VERSION = import.meta.env.VITE_APP_VERSION || '2.0.2';
|
||||
const VERSION_STORAGE_KEY = 'igny8-app-version';
|
||||
|
||||
const refreshUserData = async (force = false) => {
|
||||
const now = Date.now();
|
||||
// Throttle: only refresh if last refresh was more than 30 seconds ago (unless forced)
|
||||
if (!force && now - lastUserRefresh.current < 30000) return;
|
||||
|
||||
// Check if token exists before making API call
|
||||
const authState = useAuthStore.getState();
|
||||
if (!authState?.token) {
|
||||
// Token not available yet - wait a bit for Zustand persist to write it
|
||||
setTimeout(() => {
|
||||
const retryAuthState = useAuthStore.getState();
|
||||
if (retryAuthState?.token && retryAuthState?.isAuthenticated) {
|
||||
refreshUserData(force);
|
||||
}
|
||||
}, 100); // Wait 100ms for persist to write
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
lastUserRefresh.current = now;
|
||||
await refreshUser();
|
||||
|
||||
// Store current version after successful refresh
|
||||
if (force) {
|
||||
localStorage.setItem(VERSION_STORAGE_KEY, APP_VERSION);
|
||||
}
|
||||
} catch (error) {
|
||||
// Silently fail - user might still be authenticated
|
||||
console.debug('User data refresh failed (non-critical):', error);
|
||||
}
|
||||
};
|
||||
|
||||
// Check if app version changed (indicates code update)
|
||||
const storedVersion = localStorage.getItem(VERSION_STORAGE_KEY);
|
||||
if (storedVersion !== APP_VERSION) {
|
||||
// Force refresh on version change
|
||||
refreshUserData(true);
|
||||
} else {
|
||||
// Normal refresh on mount
|
||||
refreshUserData();
|
||||
}
|
||||
|
||||
// Refresh when window becomes visible (user switches back to tab)
|
||||
const handleVisibilityChange = () => {
|
||||
if (document.visibilityState === 'visible') {
|
||||
refreshUserData();
|
||||
}
|
||||
};
|
||||
|
||||
// Refresh on window focus
|
||||
const handleFocus = () => {
|
||||
refreshUserData();
|
||||
};
|
||||
|
||||
// Proactive token refresh - refresh token every 12 minutes (before 15-minute expiry)
|
||||
// This prevents 401 errors and ensures seamless user experience
|
||||
const tokenRefreshInterval = setInterval(async () => {
|
||||
const authState = useAuthStore.getState();
|
||||
const refreshToken = authState?.refreshToken;
|
||||
if (refreshToken && authState?.isAuthenticated) {
|
||||
try {
|
||||
await authState.refreshToken();
|
||||
console.debug('Token proactively refreshed');
|
||||
} catch (error) {
|
||||
console.debug('Proactive token refresh failed (will retry on next API call):', error);
|
||||
}
|
||||
}
|
||||
}, 720000); // 12 minutes = 720000ms
|
||||
|
||||
// Periodic user data refresh every 2 minutes
|
||||
const intervalId = setInterval(() => refreshUserData(), 120000);
|
||||
|
||||
document.addEventListener('visibilitychange', handleVisibilityChange);
|
||||
window.addEventListener('focus', handleFocus);
|
||||
|
||||
return () => {
|
||||
clearInterval(tokenRefreshInterval);
|
||||
clearInterval(intervalId);
|
||||
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
||||
window.removeEventListener('focus', handleFocus);
|
||||
};
|
||||
}, [isAuthenticated, refreshUser]);
|
||||
// All session refresh logic removed - API interceptor handles token refresh automatically on 401
|
||||
|
||||
// Load credit balance and set in header metrics
|
||||
useEffect(() => {
|
||||
|
||||
Reference in New Issue
Block a user