logo out issues fixes
This commit is contained in:
@@ -54,7 +54,7 @@ export const useAuthStore = create<AuthState>()(
|
||||
isAuthenticated: false,
|
||||
loading: false, // Always start with loading false - will be set true only during login/register
|
||||
|
||||
login: async (email, password) => {
|
||||
login: async (email, password, rememberMe = false) => {
|
||||
set({ loading: true });
|
||||
try {
|
||||
const API_BASE_URL = import.meta.env.VITE_BACKEND_URL || 'https://api.igny8.com/api';
|
||||
@@ -63,7 +63,7 @@ export const useAuthStore = create<AuthState>()(
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ email, password }),
|
||||
body: JSON.stringify({ email, password, remember_me: rememberMe }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
@@ -144,6 +144,37 @@ export const useAuthStore = create<AuthState>()(
|
||||
},
|
||||
|
||||
logout: () => {
|
||||
// Check if there's already a logout reason from automatic logout
|
||||
const existingReason = localStorage.getItem('logout_reason');
|
||||
|
||||
if (!existingReason) {
|
||||
// Only store manual logout reason if no automatic reason exists
|
||||
const currentPath = typeof window !== 'undefined' ? window.location.pathname : 'unknown';
|
||||
const logoutContext = {
|
||||
code: 'MANUAL_LOGOUT',
|
||||
message: 'You have been logged out',
|
||||
path: currentPath,
|
||||
context: {
|
||||
user: get().user?.email || 'unknown',
|
||||
timestamp: new Date().toISOString(),
|
||||
},
|
||||
timestamp: new Date().toISOString(),
|
||||
source: 'manual_user_action'
|
||||
};
|
||||
|
||||
console.error('🚪 MANUAL LOGOUT from page:', currentPath);
|
||||
|
||||
try {
|
||||
localStorage.setItem('logout_reason', JSON.stringify(logoutContext));
|
||||
console.error('✅ Stored manual logout_reason');
|
||||
} catch (e) {
|
||||
console.error('❌ Failed to store logout reason:', e);
|
||||
}
|
||||
} else {
|
||||
console.error('⚠️ Automatic logout reason already exists, not overwriting with manual logout');
|
||||
console.error('Existing reason:', existingReason);
|
||||
}
|
||||
|
||||
// CRITICAL: Properly clear ALL cookies to prevent session contamination
|
||||
const cookies = document.cookie.split(";");
|
||||
for (let i = 0; i < cookies.length; i++) {
|
||||
@@ -157,8 +188,9 @@ export const useAuthStore = create<AuthState>()(
|
||||
}
|
||||
|
||||
// IMPORTANT: Selectively clear auth-related localStorage items
|
||||
// DO NOT clear 'logout_reason' - it needs to persist for signin page display!
|
||||
// DO NOT use localStorage.clear() as it breaks Zustand persist middleware
|
||||
const authKeys = ['auth-storage', 'auth-store', 'site-storage', 'sector-storage', 'billing-storage'];
|
||||
const authKeys = ['auth-storage', 'auth-store', 'site-storage', 'sector-storage', 'billing-storage', 'access_token', 'refresh_token'];
|
||||
authKeys.forEach(key => {
|
||||
try {
|
||||
localStorage.removeItem(key);
|
||||
@@ -389,9 +421,36 @@ export const useAuthStore = create<AuthState>()(
|
||||
|
||||
const refreshedUser = response.user;
|
||||
if (!refreshedUser.account) {
|
||||
const logoutReasonData = {
|
||||
code: 'ACCOUNT_REQUIRED',
|
||||
message: 'Account not configured for this user. Please contact support.',
|
||||
path: typeof window !== 'undefined' ? window.location.pathname : 'unknown',
|
||||
context: {
|
||||
user_email: refreshedUser.email,
|
||||
user_id: refreshedUser.id
|
||||
},
|
||||
timestamp: new Date().toISOString(),
|
||||
source: 'refresh_user_validation'
|
||||
};
|
||||
console.error('🚨 LOGOUT TRIGGERED - Account Required:', logoutReasonData);
|
||||
localStorage.setItem('logout_reason', JSON.stringify(logoutReasonData));
|
||||
throw createAuthError('Account not configured for this user. Please contact support.', 'ACCOUNT_REQUIRED');
|
||||
}
|
||||
if (!refreshedUser.account.plan) {
|
||||
const logoutReasonData = {
|
||||
code: 'PLAN_REQUIRED',
|
||||
message: 'Active subscription required. Visit igny8.com/pricing to subscribe.',
|
||||
path: typeof window !== 'undefined' ? window.location.pathname : 'unknown',
|
||||
context: {
|
||||
user_email: refreshedUser.email,
|
||||
account_name: refreshedUser.account.name,
|
||||
account_id: refreshedUser.account.id
|
||||
},
|
||||
timestamp: new Date().toISOString(),
|
||||
source: 'refresh_user_validation'
|
||||
};
|
||||
console.error('🚨 LOGOUT TRIGGERED - Plan Required:', logoutReasonData);
|
||||
localStorage.setItem('logout_reason', JSON.stringify(logoutReasonData));
|
||||
throw createAuthError('Active subscription required. Visit igny8.com/pricing to subscribe.', 'PLAN_REQUIRED');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user