fix: auth state not loaded properly, console cleanup

This commit is contained in:
Jakob Stornig 2023-12-18 18:16:16 +01:00
parent f445504f10
commit 86260d7a24
4 changed files with 25 additions and 41 deletions

View file

@ -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: {

View file

@ -10,7 +10,6 @@ export default function _layout() {
initDatabase();
}, []);
console.log("layout called")
return (
<AuthProvider>
<ThemeProvider>

View file

@ -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<any>;
onLogin?: (email: string, password: string) => Promise<any>
onLogout?: () => void
onLogin?: (email: string, password: string) => Promise<any>;
onLogout?: () => void;
}
const SESSION_KEY = 'my_session'
@ -50,39 +51,32 @@ 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({
if(session !== null && email !== null){
console.log("applied session", session, email)
setAuthState( (prev) => {
return {
email: email,
authenticated: true,
session: session,
isLoading: false
})
}
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 <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
}

View file

@ -6,7 +6,6 @@ import { useAuth } from './contexts/AuthContext'
export default function index() {
const {authState} = useAuth()
return (
<Redirect href={authState?.authenticated ? "/home/" : "/login"}></Redirect>