diff --git a/app/_layout.tsx b/app/_layout.tsx index aec9501..d43e251 100644 --- a/app/_layout.tsx +++ b/app/_layout.tsx @@ -70,9 +70,9 @@ if (Platform.OS === 'ios') { } if (__DEV__) { - // functions().useEmulator("localhost", 5001); - // firestore().useEmulator("localhost", 5471); - // auth().useEmulator("http://localhost:9099"); + functions().useEmulator("localhost", 5001); + firestore().useEmulator("localhost", 5471); + auth().useEmulator("http://localhost:9099"); } type TextStyleBase = diff --git a/hooks/firebase/useSignUp.ts b/hooks/firebase/useSignUp.ts index 13f55c4..aab0f94 100644 --- a/hooks/firebase/useSignUp.ts +++ b/hooks/firebase/useSignUp.ts @@ -1,30 +1,49 @@ import {useMutation} from "@tanstack/react-query"; import auth from "@react-native-firebase/auth"; +import firestore from "@react-native-firebase/firestore"; import {ProfileType, useAuthContext} from "@/contexts/AuthContext"; import {useSetUserData} from "./useSetUserData"; import {uuidv4} from "@firebase/util"; import * as Localization from "expo-localization"; export const useSignUp = () => { - const {setRedirectOverride} = useAuthContext() + const {setRedirectOverride} = useAuthContext(); const {mutateAsync: setUserData} = useSetUserData(); + const createHouseholdIfNeeded = async (familyId: string, lastName: string) => { + try { + const householdRef = firestore().collection("Households"); + const snapshot = await householdRef.where("familyId", "==", familyId).get(); + + if (snapshot.empty) { + await householdRef.add({ + familyId, + name: lastName + }); + } + } catch (error) { + console.error("Error creating household:", error); + throw error; + } + }; + return useMutation({ mutationKey: ["signUp"], mutationFn: async ({ - email, - password, - firstName, - lastName, - birthday - }: { + email, + password, + firstName, + lastName, + birthday + }: { email: string; password: string; firstName: string; lastName: string; birthday: Date; }) => { - setRedirectOverride(true) + setRedirectOverride(true); + const familyId = uuidv4(); await auth() .createUserWithEmailAndPassword(email, password) @@ -35,12 +54,14 @@ export const useSignUp = () => { userType: ProfileType.PARENT, firstName: firstName, lastName: lastName, - familyId: uuidv4(), + familyId: familyId, timeZone: Localization.getCalendars()[0].timeZone, birthday: birthday }, customUser: res.user, }); + + await createHouseholdIfNeeded(familyId, lastName); } catch (error) { console.error(error); }