auth.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. import { GOTRUE_ERRORS, IS_PLATFORM } from './constants'
  12. import { useAiAssistantStateSnapshot } from '@/state/ai-assistant-state'
  13. const AuthErrorToaster = ({ children }: PropsWithChildren) => {
  14. const error = useAuthError()
  15. useEffect(() => {
  16. if (error !== null) {
  17. // Check for unverified GitHub users after a GitHub sign in
  18. if (error.message === GOTRUE_ERRORS.UNVERIFIED_GITHUB_USER) {
  19. toast.error(
  20. 'Please verify your email on GitHub first, then reach out to us at support@supabase.io to log into the dashboard'
  21. )
  22. return
  23. }
  24. toast.error(error.message)
  25. }
  26. }, [error])
  27. return children
  28. }
  29. export const AuthProvider = ({ children }: PropsWithChildren) => {
  30. return (
  31. <AuthProviderInternal alwaysLoggedIn={!IS_PLATFORM}>
  32. <AuthErrorToaster>{children}</AuthErrorToaster>
  33. </AuthProviderInternal>
  34. )
  35. }
  36. export function useSignOut() {
  37. const queryClient = useQueryClient()
  38. const { clearStorage: clearAssistantStorage } = useAiAssistantStateSnapshot()
  39. return useCallback(async () => {
  40. const result = await gotrueClient.signOut()
  41. posthogClient.reset()
  42. clearLocalStorage()
  43. // Clear Assistant IndexedDB
  44. await clearAssistantStorage()
  45. queryClient.clear()
  46. return result
  47. }, [queryClient, clearAssistantStorage])
  48. }