-
-
Save rodjoseph/ec6b511ea6ab0a2354b231e31121f736 to your computer and use it in GitHub Desktop.
User authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function App() { | |
const isLoadingComplete = useCachedResources(); | |
const colorScheme = useColorScheme(); | |
if (!isLoadingComplete) { | |
return null; | |
} | |
else { | |
return ( | |
<UserContextProvider> | |
<ActionSheetProvider> | |
<SafeAreaProvider> | |
<Navigation colorScheme={colorScheme} /> | |
<StatusBar /> | |
</SafeAreaProvider> | |
</ActionSheetProvider> | |
</UserContextProvider> | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createClient } from '@supabase/supabase-js' | |
import AsyncStorage from '@react-native-community/async-storage' | |
import * as Env from '../../environment' | |
export const supabase = createClient( | |
Env.default.supabaseUrl, | |
Env.default.supabaseAnonKey, | |
{ | |
localStorage: AsyncStorage as any, | |
autoRefreshToken: true, | |
persistSession: true, | |
} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useState, createContext, useContext } from 'react' | |
import { Session, User } from '@supabase/supabase-js' | |
import { supabase } from '../config/Supabase' | |
export const UserContext = createContext<{ user: User | null; session: Session | null }>({ | |
user: null, | |
session: null, | |
}) | |
export const UserContextProvider = (props: any) => { | |
const [session, setSession] = useState<Session | null>(null) | |
const [user, setUser] = useState<User | null>(null) | |
useEffect(() => { | |
const session = supabase.auth.session() | |
setSession(session) | |
console.log(session) | |
setUser(session?.user ?? null) | |
const { data: authListener } = supabase.auth.onAuthStateChange(async (event, session) => { | |
console.log(`Supabase auth event: ${event}`) | |
setSession(session) | |
setUser((session?.user) ?? null) | |
}) | |
return () => { | |
authListener!.unsubscribe() | |
} | |
}, []) | |
const value = { | |
session, | |
user, | |
} | |
return <UserContext.Provider value={value} {...props} /> | |
} | |
export const useUser = () => { | |
const context = useContext(UserContext) | |
if (context === undefined) { | |
throw new Error(`useUser must be used within a UserContextProvider.`) | |
} | |
return context | |
} | |
export default UserContextProvider; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment