buster/apps/web/src/components/features/auth/SignOutHandler.tsx

31 lines
961 B
TypeScript
Raw Normal View History

2025-09-09 05:28:35 +08:00
import { useNavigate } from '@tanstack/react-router';
2025-08-13 05:57:35 +08:00
import { useCallback } from 'react';
import { useBusterNotifications } from '@/context/BusterNotifications';
import { signOut } from '@/integrations/supabase/signOut';
import { clearAllBrowserStorage } from '@/lib/storage';
export const useSignOut = () => {
const { openErrorMessage } = useBusterNotifications();
2025-09-09 05:28:35 +08:00
const navigate = useNavigate();
2025-08-13 05:57:35 +08:00
const handleSignOut = useCallback(async () => {
try {
// Then perform server-side sign out
await signOut();
2025-09-09 06:19:19 +08:00
try {
// First clear all client-side storage
clearAllBrowserStorage();
} catch (error) {
console.error('Error clearing browser storage', error);
2025-09-09 05:28:35 +08:00
}
2025-08-13 05:57:35 +08:00
} catch (error) {
2025-09-09 06:19:19 +08:00
console.error('Error signing out', error);
2025-08-13 05:57:35 +08:00
openErrorMessage('Error signing out');
2025-09-09 06:19:19 +08:00
} finally {
navigate({ to: '/auth/login' });
2025-08-13 05:57:35 +08:00
}
2025-09-09 05:28:35 +08:00
}, [navigate, openErrorMessage]);
2025-08-13 05:57:35 +08:00
return handleSignOut;
};