diff --git a/app/(tabs)/_layout.tsx b/app/(tabs)/_layout.tsx index bbff5e2..ab3ebb5 100644 --- a/app/(tabs)/_layout.tsx +++ b/app/(tabs)/_layout.tsx @@ -13,10 +13,6 @@ export default function Layout() { // const backgroundColor: string = useThemeColor("backgroundColor"); // const tabBarColor: string = useThemeColor("tabBarColor"); const {authState} = useAuth() - useEffect(()=>{ - console.log(authState, "in root Layout") - },[authState]) - const styles = StyleSheet.create({ sceneContainer: { diff --git a/app/_layout.tsx b/app/_layout.tsx index 0a38cb4..f34a4ea 100644 --- a/app/_layout.tsx +++ b/app/_layout.tsx @@ -10,7 +10,6 @@ export default function _layout() { initDatabase(); }, []); - console.log("layout called") return ( diff --git a/app/contexts/AuthContext.tsx b/app/contexts/AuthContext.tsx index 236c962..0cf1d8d 100644 --- a/app/contexts/AuthContext.tsx +++ b/app/contexts/AuthContext.tsx @@ -4,9 +4,10 @@ import * as SecureStore from 'expo-secure-store' interface AuthProps { authState?: {email: string | null; authenticated: boolean | null; session: string | null}; + isLoading?: boolean | null; onRegister?: (email: string, password: string) => Promise; - onLogin?: (email: string, password: string) => Promise - onLogout?: () => void + onLogin?: (email: string, password: string) => Promise; + onLogout?: () => void; } const SESSION_KEY = 'my_session' @@ -50,38 +51,31 @@ export const AuthProvider = ({children}: any) => { email: string | null; authenticated: boolean | null; session : string | null; - isLoading: boolean; }>({ - email: null, authenticated: null, session: null, isLoading: false + email: null, authenticated: null, session: null }); + const [isLoading, setIsLoading] = useState(true) useEffect(() => { const loadSession = async () => { - setAuthState((prev) => { - return { - ...prev, - isLoading: true - } - }) + setIsLoading(true) const session = await getStorageItemAsync(SESSION_KEY) const email = await getStorageItemAsync(USER_KEY) - if(session && email){ - setAuthState({ - email: email, - authenticated: true, - session: session, - isLoading: false + if(session !== null && email !== null){ + console.log("applied session", session, email) + setAuthState( (prev) => { + return { + email: email, + authenticated: true, + session: session, + } }) } - setAuthState((prev) => { - return { - ...prev, - isLoading: false - } - }) + + setIsLoading(false) } loadSession() },[]) @@ -92,29 +86,23 @@ export const AuthProvider = ({children}: any) => { } const login = async (email: string, password: string) => { - + setIsLoading(true) //login functionality goes here console.log("login called") - setAuthState((prev) => { - return{ - ...prev, - isLoading: true - } - }) setAuthState({ email: email, authenticated: true, - session: "super duper session token das wir vlt mal brauchen", - isLoading: false - + session: "super duper session token das wir vlt mal brauchen" }) await setStorageItemAsync(SESSION_KEY, "super duper session token das wir vlt mal brauchen") await setStorageItemAsync(USER_KEY, email) + setIsLoading(false) } const logout = async () => { + setIsLoading(true) await setStorageItemAsync(SESSION_KEY, null) await setStorageItemAsync(USER_KEY, null) @@ -122,8 +110,9 @@ export const AuthProvider = ({children}: any) => { email: null, authenticated: false, session: null, - isLoading: false }); + + setIsLoading(false) }; @@ -131,7 +120,8 @@ export const AuthProvider = ({children}: any) => { onRegister: register, onLogin: login, onLogout: logout, - authState + isLoading: isLoading, + authState: authState } return {children} } \ No newline at end of file diff --git a/app/index.tsx b/app/index.tsx index 094049b..265d68b 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -6,9 +6,8 @@ import { useAuth } from './contexts/AuthContext' export default function index() { const {authState} = useAuth() - return ( - + ) } \ No newline at end of file