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
|
|
@ -13,10 +13,6 @@ export default function Layout() {
|
||||||
// const backgroundColor: string = useThemeColor("backgroundColor");
|
// const backgroundColor: string = useThemeColor("backgroundColor");
|
||||||
// const tabBarColor: string = useThemeColor("tabBarColor");
|
// const tabBarColor: string = useThemeColor("tabBarColor");
|
||||||
const {authState} = useAuth()
|
const {authState} = useAuth()
|
||||||
useEffect(()=>{
|
|
||||||
console.log(authState, "in root Layout")
|
|
||||||
},[authState])
|
|
||||||
|
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
sceneContainer: {
|
sceneContainer: {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ export default function _layout() {
|
||||||
initDatabase();
|
initDatabase();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
console.log("layout called")
|
|
||||||
return (
|
return (
|
||||||
<AuthProvider>
|
<AuthProvider>
|
||||||
<ThemeProvider>
|
<ThemeProvider>
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,10 @@ import * as SecureStore from 'expo-secure-store'
|
||||||
|
|
||||||
interface AuthProps {
|
interface AuthProps {
|
||||||
authState?: {email: string | null; authenticated: boolean | null; session: string | null};
|
authState?: {email: string | null; authenticated: boolean | null; session: string | null};
|
||||||
|
isLoading?: boolean | null;
|
||||||
onRegister?: (email: string, password: string) => Promise<any>;
|
onRegister?: (email: string, password: string) => Promise<any>;
|
||||||
onLogin?: (email: string, password: string) => Promise<any>
|
onLogin?: (email: string, password: string) => Promise<any>;
|
||||||
onLogout?: () => void
|
onLogout?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const SESSION_KEY = 'my_session'
|
const SESSION_KEY = 'my_session'
|
||||||
|
|
@ -50,38 +51,31 @@ export const AuthProvider = ({children}: any) => {
|
||||||
email: string | null;
|
email: string | null;
|
||||||
authenticated: boolean | null;
|
authenticated: boolean | null;
|
||||||
session : string | 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(() => {
|
useEffect(() => {
|
||||||
const loadSession = async () => {
|
const loadSession = async () => {
|
||||||
setAuthState((prev) => {
|
setIsLoading(true)
|
||||||
return {
|
|
||||||
...prev,
|
|
||||||
isLoading: true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
const session = await getStorageItemAsync(SESSION_KEY)
|
const session = await getStorageItemAsync(SESSION_KEY)
|
||||||
const email = await getStorageItemAsync(USER_KEY)
|
const email = await getStorageItemAsync(USER_KEY)
|
||||||
|
|
||||||
if(session && email){
|
if(session !== null && email !== null){
|
||||||
setAuthState({
|
console.log("applied session", session, email)
|
||||||
email: email,
|
setAuthState( (prev) => {
|
||||||
authenticated: true,
|
return {
|
||||||
session: session,
|
email: email,
|
||||||
isLoading: false
|
authenticated: true,
|
||||||
|
session: session,
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
setAuthState((prev) => {
|
|
||||||
return {
|
setIsLoading(false)
|
||||||
...prev,
|
|
||||||
isLoading: false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
loadSession()
|
loadSession()
|
||||||
},[])
|
},[])
|
||||||
|
|
@ -92,29 +86,23 @@ export const AuthProvider = ({children}: any) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const login = async (email: string, password: string) => {
|
const login = async (email: string, password: string) => {
|
||||||
|
setIsLoading(true)
|
||||||
//login functionality goes here
|
//login functionality goes here
|
||||||
console.log("login called")
|
console.log("login called")
|
||||||
setAuthState((prev) => {
|
|
||||||
return{
|
|
||||||
...prev,
|
|
||||||
isLoading: true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
setAuthState({
|
setAuthState({
|
||||||
email: email,
|
email: email,
|
||||||
authenticated: true,
|
authenticated: true,
|
||||||
session: "super duper session token das wir vlt mal brauchen",
|
session: "super duper session token das wir vlt mal brauchen"
|
||||||
isLoading: false
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
await setStorageItemAsync(SESSION_KEY, "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)
|
await setStorageItemAsync(USER_KEY, email)
|
||||||
|
setIsLoading(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
const logout = async () => {
|
const logout = async () => {
|
||||||
|
setIsLoading(true)
|
||||||
await setStorageItemAsync(SESSION_KEY, null)
|
await setStorageItemAsync(SESSION_KEY, null)
|
||||||
await setStorageItemAsync(USER_KEY, null)
|
await setStorageItemAsync(USER_KEY, null)
|
||||||
|
|
||||||
|
|
@ -122,8 +110,9 @@ export const AuthProvider = ({children}: any) => {
|
||||||
email: null,
|
email: null,
|
||||||
authenticated: false,
|
authenticated: false,
|
||||||
session: null,
|
session: null,
|
||||||
isLoading: false
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setIsLoading(false)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -131,7 +120,8 @@ export const AuthProvider = ({children}: any) => {
|
||||||
onRegister: register,
|
onRegister: register,
|
||||||
onLogin: login,
|
onLogin: login,
|
||||||
onLogout: logout,
|
onLogout: logout,
|
||||||
authState
|
isLoading: isLoading,
|
||||||
|
authState: authState
|
||||||
}
|
}
|
||||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
|
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
|
||||||
}
|
}
|
||||||
|
|
@ -6,9 +6,8 @@ import { useAuth } from './contexts/AuthContext'
|
||||||
export default function index() {
|
export default function index() {
|
||||||
const {authState} = useAuth()
|
const {authState} = useAuth()
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Redirect href={authState?.authenticated ? "/home/" : "/login"}></Redirect>
|
<Redirect href={authState?.authenticated ? "/home/" : "/login"}></Redirect>
|
||||||
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Reference in a new issue