-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathuseLogout.ts
More file actions
45 lines (42 loc) · 1.96 KB
/
Copy pathuseLogout.ts
File metadata and controls
45 lines (42 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { useCallback } from 'react';
import { useStore } from '@/hooks/useStore';
import { ErrorLogger } from '@/utils/error-logger';
/**
* Custom hook to handle logout functionality
* Clears all session and local storage to reset the session
* @returns {Function} handleLogout - Function to trigger the logout process
*/
export const useLogout = () => {
const { client } = useStore() ?? {};
return useCallback(async () => {
try {
// Call the client store logout method which clears all storage
await client?.logout();
// Analytics.reset() removed - Analytics package has been removed from the project
// See migrate-docs/MONITORING_PACKAGES.md for re-enabling analytics if needed
} catch (error) {
ErrorLogger.error('Logout', 'Logout failed', error);
// If logout fails, clear only auth-related storage keys
// This preserves user preferences (theme, language, etc.) while ensuring auth data is cleared
try {
// Clear auth-related sessionStorage items
sessionStorage.removeItem('auth_info');
// Clear auth-related localStorage items
localStorage.removeItem('active_loginid');
localStorage.removeItem('authToken');
localStorage.removeItem('accountsList');
localStorage.removeItem('clientAccounts');
localStorage.removeItem('account_type');
} catch (storageError) {
ErrorLogger.error('Logout', 'Failed to clear auth storage', storageError);
// Last resort: if targeted clearing fails, clear all storage
try {
sessionStorage.clear();
localStorage.clear();
} catch (finalError) {
ErrorLogger.error('Logout', 'Failed to clear all storage', finalError);
}
}
}
}, [client]);
};