import { AuthContext } from '@/hooks/use-auth-context' import { supabase } from '@/lib/supabase' import { PropsWithChildren, useEffect, useState } from 'react' export default function AuthProvider({ children }: PropsWithChildren) { const [claims, setClaims] = useState | undefined | null>() const [profile, setProfile] = useState() const [isLoading, setIsLoading] = useState(true) // Fetch the claims once, and subscribe to auth state changes useEffect(() => { const fetchClaims = async () => { setIsLoading(true) const { data, error } = await supabase.auth.getClaims() if (error) { console.error('Error fetching claims:', error) } setClaims(data?.claims ?? null) setIsLoading(false) } fetchClaims() const { data: { subscription }, } = supabase.auth.onAuthStateChange(async (_event, _session) => { console.log('Auth state changed:', { event: _event }) const { data } = await supabase.auth.getClaims() setClaims(data?.claims ?? null) }) // Cleanup subscription on unmount return () => { subscription.unsubscribe() } }, []) // Fetch the profile when the claims change useEffect(() => { const fetchProfile = async () => { setIsLoading(true) if (claims) { const { data } = await supabase.from('profiles').select('*').eq('id', claims.sub).single() setProfile(data) } else { setProfile(null) } setIsLoading(false) } fetchProfile() }, [claims]) return ( {children} ) }