fix: auth state not loaded properly, console cleanup
This commit is contained in:
parent
f445504f10
commit
86260d7a24
4 changed files with 25 additions and 41 deletions
|
|
@ -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,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 <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
|
||||
}
|
||||
Reference in a new issue