Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 46 47 48 49 | 1x 1x 1x 1x 15x 15x 15x 8x 8x 1x 1x 7x 7x 7x 5x 4x 4x 4x 4x 3x 3x 15x | import { useState, useCallback } from 'react';
import { logout } from '@/services/authService';
import { ApiError } from '@/services/api';
export interface UseLogoutResult {
isLoggingOut: boolean;
logoutError: string | null;
handleLogout: () => Promise<void>;
}
export function useLogoutViewModel(
refreshToken: string | null,
onLoggedOut: () => void,
): UseLogoutResult {
const [isLoggingOut, setIsLoggingOut] = useState(false);
const [logoutError, setLogoutError] = useState<string | null>(null);
const handleLogout = useCallback(async () => {
setLogoutError(null);
if (!refreshToken) {
onLoggedOut();
return;
}
setIsLoggingOut(true);
try {
await logout({ refresh_token: refreshToken });
} catch (err) {
if (err instanceof ApiError && err.status === 401) {
// Refresh token already invalid on server; still clear local session.
} else {
const message =
err instanceof ApiError
? err.message
: 'Could not reach the server. Please try again.';
setLogoutError(message);
setIsLoggingOut(false);
return;
}
}
onLoggedOut();
setIsLoggingOut(false);
}, [refreshToken, onLoggedOut]);
return { isLoggingOut, logoutError, handleLogout };
}
|