mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
This PR runs `prettier` on the `examples` folder. Depends on https://github.com/supabase/supabase/pull/43849.
73 lines
1.7 KiB
TypeScript
73 lines
1.7 KiB
TypeScript
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<Record<string, any> | undefined | null>()
|
|
const [profile, setProfile] = useState<any>()
|
|
const [isLoading, setIsLoading] = useState<boolean>(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 (
|
|
<AuthContext.Provider
|
|
value={{
|
|
claims,
|
|
isLoading,
|
|
profile,
|
|
isLoggedIn: claims != undefined,
|
|
}}
|
|
>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
)
|
|
}
|