auth.tsx57 lines · main
| 1 | import { useQueryClient } from '@tanstack/react-query' |
| 2 | import { |
| 3 | AuthProvider as AuthProviderInternal, |
| 4 | clearLocalStorage, |
| 5 | gotrueClient, |
| 6 | posthogClient, |
| 7 | useAuthError, |
| 8 | } from 'common' |
| 9 | import { PropsWithChildren, useCallback, useEffect } from 'react' |
| 10 | import { toast } from 'sonner' |
| 11 | |
| 12 | import { GOTRUE_ERRORS, IS_PLATFORM } from './constants' |
| 13 | import { useAiAssistantStateSnapshot } from '@/state/ai-assistant-state' |
| 14 | |
| 15 | const AuthErrorToaster = ({ children }: PropsWithChildren) => { |
| 16 | const error = useAuthError() |
| 17 | |
| 18 | useEffect(() => { |
| 19 | if (error !== null) { |
| 20 | // Check for unverified GitHub users after a GitHub sign in |
| 21 | if (error.message === GOTRUE_ERRORS.UNVERIFIED_GITHUB_USER) { |
| 22 | toast.error( |
| 23 | 'Please verify your email on GitHub first, then reach out to us at support@supabase.io to log into the dashboard' |
| 24 | ) |
| 25 | return |
| 26 | } |
| 27 | |
| 28 | toast.error(error.message) |
| 29 | } |
| 30 | }, [error]) |
| 31 | |
| 32 | return children |
| 33 | } |
| 34 | |
| 35 | export const AuthProvider = ({ children }: PropsWithChildren) => { |
| 36 | return ( |
| 37 | <AuthProviderInternal alwaysLoggedIn={!IS_PLATFORM}> |
| 38 | <AuthErrorToaster>{children}</AuthErrorToaster> |
| 39 | </AuthProviderInternal> |
| 40 | ) |
| 41 | } |
| 42 | |
| 43 | export function useSignOut() { |
| 44 | const queryClient = useQueryClient() |
| 45 | const { clearStorage: clearAssistantStorage } = useAiAssistantStateSnapshot() |
| 46 | |
| 47 | return useCallback(async () => { |
| 48 | const result = await gotrueClient.signOut() |
| 49 | posthogClient.reset() |
| 50 | clearLocalStorage() |
| 51 | // Clear Assistant IndexedDB |
| 52 | await clearAssistantStorage() |
| 53 | queryClient.clear() |
| 54 | |
| 55 | return result |
| 56 | }, [queryClient, clearAssistantStorage]) |
| 57 | } |