import { Session, SessionContext as SessionContextHelper } from '@supabase/auth-helpers-react' import { AuthError, User } from '@supabase/supabase-js' import { supabase } from 'app/utils/supabase/client.native' import { router, useSegments } from 'expo-router' import { createContext, useEffect, useState } from 'react' import { Platform } from 'react-native' import { AuthProviderProps } from './AuthProvider' import { AuthStateChangeHandler } from './AuthStateChangeHandler' export const SessionContext = createContext({ session: null, error: null, isLoading: false, supabaseClient: supabase, }) export const AuthProvider = ({ children, initialSession }: AuthProviderProps) => { const [session, setSession] = useState(initialSession || null) const [error, setError] = useState(null) const [isLoading, setIsLoading] = useState(false) useProtectedRoute(session?.user ?? null) useEffect(() => { setIsLoading(true) supabase.auth .getSession() .then(({ data: { session: newSession } }) => { setSession(newSession) }) .catch((error) => setError(new AuthError(error.message))) .finally(() => setIsLoading(false)) }, []) useEffect(() => { const { data: { subscription }, } = supabase.auth.onAuthStateChange((_event, newSession) => { setSession(newSession) }) return () => { subscription.unsubscribe() } }, []) return ( {children} ) } export function useProtectedRoute(user: User | null) { const segments = useSegments() useEffect(() => { const inAuthGroup = segments[0] === '(auth)' // if ( // // If the user is not signed in and the initial segment is not anything in the auth group. // !user && // !inAuthGroup // ) { // // Redirect to the sign-in page. // replaceRoute('/onboarding') // } else if (user && inAuthGroup) { // // Redirect away from the sign-in page. // replaceRoute('/') // } }, [user, segments]) } /** * temporary fix * * see https://github.com/expo/router/issues/740 * see https://github.com/expo/router/issues/745 * */ const replaceRoute = (href: string) => { if (Platform.OS === 'ios') { setTimeout(() => { router.replace(href) }, 1) } else { setImmediate(() => { router.replace(href) }) } }