mirror of
https://github.com/urosran/cally.git
synced 2025-07-14 17:25:46 +00:00

- Fixed an issue with the profile data not being read from db becaue of the local state not being updated instantly
92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import {createContext, FC, ReactNode, useContext, useEffect, useState} from "react";
|
|
import * as SplashScreen from "expo-splash-screen";
|
|
import auth, {FirebaseAuthTypes} from "@react-native-firebase/auth";
|
|
import {useRouter} from "expo-router";
|
|
import firestore from "@react-native-firebase/firestore";
|
|
import {UserProfile} from "@/hooks/firebase/types/profileTypes";
|
|
|
|
export enum ProfileType {
|
|
"PARENT" = "parent",
|
|
"CHILD" = "child",
|
|
"CAREGIVER" = "caregiver"
|
|
}
|
|
|
|
interface IAuthContext {
|
|
user: FirebaseAuthTypes.User | null,
|
|
profileType?: ProfileType,
|
|
profileData?: UserProfile,
|
|
setProfileData: (profileData: UserProfile) => void,
|
|
refreshProfileData: () => Promise<void>
|
|
}
|
|
|
|
const AuthContext = createContext<IAuthContext>(undefined!)
|
|
|
|
export const AuthContextProvider: FC<{ children: ReactNode }> = ({children}) => {
|
|
const [user, setUser] = useState<FirebaseAuthTypes.User | null>(null)
|
|
const [initializing, setInitializing] = useState(true);
|
|
const [profileType, setProfileType] = useState<ProfileType | undefined>(undefined);
|
|
const [profileData, setProfileData] = useState<UserProfile | undefined>(undefined);
|
|
|
|
const {replace} = useRouter()
|
|
const ready = !initializing
|
|
|
|
const onAuthStateChangedHandler = async (authUser: FirebaseAuthTypes.User | null) => {
|
|
setUser(authUser);
|
|
|
|
if (authUser) {
|
|
await refreshProfileData(authUser);
|
|
}
|
|
|
|
if (initializing) setInitializing(false);
|
|
}
|
|
|
|
const refreshProfileData = async (authUser: FirebaseAuthTypes.User) => {
|
|
if (authUser) {
|
|
try {
|
|
const documentSnapshot = await firestore()
|
|
.collection("Profiles")
|
|
.doc(authUser.uid)
|
|
.get();
|
|
if (documentSnapshot.exists) {
|
|
setProfileType(documentSnapshot.data()?.userType);
|
|
setProfileData(documentSnapshot.data() as UserProfile);
|
|
}
|
|
} catch (error) {
|
|
setProfileType(undefined);
|
|
setProfileData(undefined);
|
|
}
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
const subscriber = auth().onAuthStateChanged(onAuthStateChangedHandler);
|
|
return subscriber;
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (!initializing) {
|
|
SplashScreen.hideAsync();
|
|
}
|
|
}, [initializing]);
|
|
|
|
useEffect(() => {
|
|
if (ready && user) {
|
|
replace({pathname: "/(auth)/calendar"})
|
|
} else if (ready && !user) {
|
|
replace({pathname: "/(unauth)"})
|
|
}
|
|
}, [user, ready]);
|
|
|
|
if (!ready) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<AuthContext.Provider value={{user, profileType, profileData, setProfileData, refreshProfileData}}>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
)
|
|
}
|
|
|
|
export const useAuthContext = () => useContext(AuthContext)!;
|