diff --git a/hooks/firebase/useAuth.ts b/hooks/firebase/useAuth.ts index 8bb9354..8c7fe74 100644 --- a/hooks/firebase/useAuth.ts +++ b/hooks/firebase/useAuth.ts @@ -1,4 +1,5 @@ import { useState, useEffect } from "react"; +import { useMutation } from "react-query"; import auth from "@react-native-firebase/auth"; import firestore from "@react-native-firebase/firestore"; import { @@ -11,6 +12,25 @@ import { type ProfileType = "parent" | "child" | "caregiver" | null; const useAuth = () => { + const createUserMutation = useMutation(() => + auth().createUserWithEmailAndPassword(email, password) + ); + const signInMutation = useMutation(() => + auth().signInWithEmailAndPassword(email, password) + ); + const loginMutation = useMutation(() => + auth().signInWithEmailAndPassword(email, password) + ); + const signOutMutation = useMutation(() => auth().signOut()); + /*const setProfileDataMutation = useMutation((profileData) => { + const currentUser = auth().currentUser; + if (currentUser) { + return firestore() + .collection("Profiles") + .doc(currentUser.uid) + .set(profileData); + } + });*/ const [user, setUser] = useState(null); const [profileType, setProfileType] = useState(null); const [email, setEmail] = useState(""); @@ -55,13 +75,13 @@ const useAuth = () => { const handleRegister = async () => { try { - await auth().createUserWithEmailAndPassword(email, password); - console.log("user registered!"); + await createUserMutation.mutateAsync(); + console.log("User registered!"); - await auth().signInWithEmailAndPassword(email, password); - console.log("user signed in!"); + await signInMutation.mutateAsync(); + console.log("User signed in!"); - let profileData: ParentProfile | ChildProfile | CaregiverProfile; + /*let profileData: ParentProfile | ChildProfile | CaregiverProfile; switch (profileType) { case "parent": profileData = { @@ -96,7 +116,7 @@ const useAuth = () => { .doc(currentUser.uid) .set(profileData); console.log("Profile document added!"); - } + }*/ } catch (error) { console.error("Error during registration: ", error); } @@ -104,7 +124,7 @@ const useAuth = () => { const handleLogin = async () => { try { - await auth().signInWithEmailAndPassword(email, password); + await loginMutation.mutateAsync(); console.log("User signed in!"); } catch (error) { console.error("Error during sign in:", error); @@ -113,7 +133,7 @@ const useAuth = () => { const handleSignOut = async () => { try { - await auth().signOut(); + await signOutMutation.mutateAsync(); console.log("User signed out!"); } catch (error) { console.error("Error during sign out:", error);