32 lines
820 B
TypeScript
32 lines
820 B
TypeScript
|
|
import { useState } from 'react';
|
||
|
|
import { useNavigate } from 'react-router-dom';
|
||
|
|
import { logout } from '../services/authService';
|
||
|
|
import { useAuthStore } from './useAuth';
|
||
|
|
|
||
|
|
export function useLogout() {
|
||
|
|
const [loading, setLoading] = useState(false);
|
||
|
|
const [error, setError] = useState<Error | null>(null);
|
||
|
|
const navigate = useNavigate();
|
||
|
|
const clearAuth = useAuthStore((state) => state.clearAuth);
|
||
|
|
|
||
|
|
const handleLogout = async () => {
|
||
|
|
try {
|
||
|
|
setLoading(true);
|
||
|
|
setError(null);
|
||
|
|
await logout();
|
||
|
|
clearAuth();
|
||
|
|
navigate('/login');
|
||
|
|
} catch (err) {
|
||
|
|
setError(err as Error);
|
||
|
|
// Même en cas d'erreur, on nettoie l'état local
|
||
|
|
clearAuth();
|
||
|
|
navigate('/login');
|
||
|
|
} finally {
|
||
|
|
setLoading(false);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
return { handleLogout, loading, error };
|
||
|
|
}
|
||
|
|
|