Revert "messy logout fixing"

This reverts commit 4fb3a144d7.
This commit is contained in:
alorig
2025-12-15 17:24:07 +05:00
parent 4fb3a144d7
commit 25f1c32366
27 changed files with 95 additions and 4396 deletions

View File

@@ -5,7 +5,6 @@
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
import { fetchAPI } from '../services/api';
import { trackLogout } from '../services/logoutTracker';
type AuthErrorCode = 'ACCOUNT_REQUIRED' | 'PLAN_REQUIRED' | 'AUTH_FAILED';
@@ -38,8 +37,8 @@ interface AuthState {
loading: boolean;
// Actions
login: (email: string, password: string, rememberMe?: boolean) => Promise<void>;
logout: (reason?: string, type?: 'USER_ACTION' | 'TOKEN_EXPIRED' | 'REFRESH_FAILED' | 'AUTH_ERROR' | 'UNKNOWN') => void;
login: (email: string, password: string) => Promise<void>;
logout: () => void;
register: (data: any) => Promise<void>;
setUser: (user: User | null) => void;
setToken: (token: string | null) => void;
@@ -55,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, rememberMe = false) => {
login: async (email, password) => {
set({ loading: true });
try {
const API_BASE_URL = import.meta.env.VITE_BACKEND_URL || 'https://api.igny8.com/api';
@@ -64,12 +63,7 @@ export const useAuthStore = create<AuthState>()(
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
password,
remember_me: rememberMe,
device_id: localStorage.getItem('device_id') || crypto.randomUUID()
}),
body: JSON.stringify({ email, password }),
});
const data = await response.json();
@@ -149,17 +143,7 @@ export const useAuthStore = create<AuthState>()(
}
},
logout: (reason = 'User clicked logout', type: 'USER_ACTION' | 'TOKEN_EXPIRED' | 'REFRESH_FAILED' | 'AUTH_ERROR' | 'UNKNOWN' = 'USER_ACTION') => {
// Track logout with detailed context
const currentState = get();
trackLogout(reason, type, {
hasToken: !!currentState.token,
hasRefreshToken: !!currentState.refreshToken,
isAuthenticated: currentState.isAuthenticated,
userId: currentState.user?.id,
userEmail: currentState.user?.email,
});
logout: () => {
// CRITICAL: Properly clear ALL cookies to prevent session contamination
const cookies = document.cookie.split(";");
for (let i = 0; i < cookies.length; i++) {
@@ -183,12 +167,8 @@ export const useAuthStore = create<AuthState>()(
}
});
// Clear sessionStorage (except logout tracking)
const logoutReason = sessionStorage.getItem('last_logout_reason');
// Clear sessionStorage
sessionStorage.clear();
if (logoutReason) {
sessionStorage.setItem('last_logout_reason', logoutReason);
}
// Reset auth state to initial values
set({