|
| 1 | +'use client' |
| 2 | + |
| 3 | +/** |
| 4 | + * Holds global app data like user. |
| 5 | + */ |
| 6 | + |
| 7 | +import { User } from '@supabase/supabase-js' |
| 8 | +import { |
| 9 | + createContext, |
| 10 | + PropsWithChildren, |
| 11 | + useCallback, |
| 12 | + useContext, |
| 13 | + useEffect, |
| 14 | + useState, |
| 15 | +} from 'react' |
| 16 | +import { createClient } from '~/utils/supabase/client' |
| 17 | + |
| 18 | +export type AppProps = PropsWithChildren |
| 19 | + |
| 20 | +export default function AppProvider({ children }: AppProps) { |
| 21 | + const [isLoadingUser, setIsLoadingUser] = useState(true) |
| 22 | + const [user, setUser] = useState<User>() |
| 23 | + |
| 24 | + const supabase = createClient() |
| 25 | + |
| 26 | + const loadUser = useCallback(async () => { |
| 27 | + setIsLoadingUser(true) |
| 28 | + try { |
| 29 | + const { data, error } = await supabase.auth.getUser() |
| 30 | + |
| 31 | + if (error) { |
| 32 | + // TODO: handle error |
| 33 | + setUser(undefined) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + const { user } = data |
| 38 | + |
| 39 | + setUser(user) |
| 40 | + |
| 41 | + return user |
| 42 | + } finally { |
| 43 | + setIsLoadingUser(false) |
| 44 | + } |
| 45 | + }, [supabase]) |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + loadUser() |
| 49 | + }, [loadUser]) |
| 50 | + |
| 51 | + const signIn = useCallback(async () => { |
| 52 | + const { error } = await supabase.auth.signInWithOAuth({ |
| 53 | + provider: 'github', |
| 54 | + options: { |
| 55 | + redirectTo: window.location.toString(), |
| 56 | + }, |
| 57 | + }) |
| 58 | + |
| 59 | + if (error) { |
| 60 | + // TODO: handle sign in error |
| 61 | + } |
| 62 | + |
| 63 | + const user = await loadUser() |
| 64 | + return user |
| 65 | + }, [supabase, loadUser]) |
| 66 | + |
| 67 | + const signOut = useCallback(async () => { |
| 68 | + const { error } = await supabase.auth.signOut() |
| 69 | + |
| 70 | + if (error) { |
| 71 | + // TODO: handle sign out error |
| 72 | + } |
| 73 | + |
| 74 | + setUser(undefined) |
| 75 | + }, [supabase]) |
| 76 | + |
| 77 | + return ( |
| 78 | + <AppContext.Provider |
| 79 | + value={{ |
| 80 | + user, |
| 81 | + isLoadingUser, |
| 82 | + signIn, |
| 83 | + signOut, |
| 84 | + }} |
| 85 | + > |
| 86 | + {children} |
| 87 | + </AppContext.Provider> |
| 88 | + ) |
| 89 | +} |
| 90 | + |
| 91 | +export type AppContextValues = { |
| 92 | + user?: User |
| 93 | + isLoadingUser: boolean |
| 94 | + signIn: () => Promise<User | undefined> |
| 95 | + signOut: () => Promise<void> |
| 96 | +} |
| 97 | + |
| 98 | +export const AppContext = createContext<AppContextValues | undefined>(undefined) |
| 99 | + |
| 100 | +export function useApp() { |
| 101 | + const context = useContext(AppContext) |
| 102 | + |
| 103 | + if (!context) { |
| 104 | + throw new Error('AppContext missing. Are you accessing useApp() outside of an AppProvider?') |
| 105 | + } |
| 106 | + |
| 107 | + return context |
| 108 | +} |
0 commit comments