import { useQueryClient } from '@tanstack/react-query' import { AuthProvider as AuthProviderInternal, clearLocalStorage, gotrueClient, posthogClient, resetServerTelemetryCookies, useAuthError, } from 'common' import { PropsWithChildren, useCallback, useEffect } from 'react' import { toast } from 'sonner' import { GOTRUE_ERRORS, IS_PLATFORM } from './constants' import { useAiAssistantStateSnapshot } from '@/state/ai-assistant-state' const AuthErrorToaster = ({ children }: PropsWithChildren) => { const error = useAuthError() useEffect(() => { if (error !== null) { // Check for unverified GitHub users after a GitHub sign in if (error.message === GOTRUE_ERRORS.UNVERIFIED_GITHUB_USER) { toast.error( 'Please verify your email on GitHub first, then reach out to us at support@supabase.io to log into the dashboard' ) return } toast.error(error.message) } }, [error]) return children } export const AuthProvider = ({ children }: PropsWithChildren) => { return ( {children} ) } export function useSignOut() { const queryClient = useQueryClient() const { clearStorage: clearAssistantStorage } = useAiAssistantStateSnapshot() return useCallback(async () => { const { data, error } = await gotrueClient.getSession() const wasSignedIn = data.session !== null || error !== null const result = await gotrueClient.signOut() // withAuth also calls this for visitors with no session, whose anonymous id cookie must // survive until sign-in links it to their account if (wasSignedIn) await resetServerTelemetryCookies() posthogClient.reset() clearLocalStorage() // Clear Assistant IndexedDB await clearAssistantStorage() queryClient.clear() return result }, [queryClient, clearAssistantStorage]) }