mirror of
https://github.com/urosran/cally.git
synced 2026-10-23 07:27:55 +00:00
Auth logic rework
This commit is contained in:
@@ -3,6 +3,7 @@ import {useMutation} from "react-query";
|
||||
import auth from "@react-native-firebase/auth";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import {User,} from "./types/profileTypes";
|
||||
import {useSignUp} from "@/hooks/firebase/useSignUp";
|
||||
|
||||
type ProfileType = "parent" | "child" | "caregiver" | null;
|
||||
|
||||
@@ -17,6 +18,8 @@ const useAuth = () => {
|
||||
auth().signInWithEmailAndPassword(email, password)
|
||||
);
|
||||
const signOutMutation = useMutation(() => auth().signOut());
|
||||
const {mutateAsync: signUp } = useSignUp()
|
||||
|
||||
/*const setProfileDataMutation = useMutation((profileData) => {
|
||||
const currentUser = auth().currentUser;
|
||||
if (currentUser) {
|
||||
@@ -35,11 +38,12 @@ const useAuth = () => {
|
||||
const handleRegister = async () => {
|
||||
try {
|
||||
|
||||
await createUserMutation.mutateAsync();
|
||||
await signUp({email, password})
|
||||
// await createUserMutation.mutateAsync();
|
||||
console.log("User registered!");
|
||||
|
||||
await signInMutation.mutateAsync();
|
||||
console.log("User signed in!");
|
||||
// await signInMutation.mutateAsync();
|
||||
// console.log("User signed in!");
|
||||
|
||||
/*let profileData: ParentProfile | ChildProfile | CaregiverProfile;
|
||||
switch (profileType) {
|
||||
|
||||
@@ -1,12 +1,23 @@
|
||||
import {useMutation} from "react-query";
|
||||
import {useMutation, useQueryClient} from "react-query";
|
||||
import {UserProfile} from "@/hooks/firebase/types/profileTypes";
|
||||
import functions from '@react-native-firebase/functions';
|
||||
import {ProfileType, useAuthContext} from "@/contexts/AuthContext";
|
||||
|
||||
export const useCreateSubUser = () => {
|
||||
const queryClient = useQueryClient()
|
||||
const { profileType } = useAuthContext()
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["createSubUser"],
|
||||
mutationFn: async ({email, password, ...userProfile}: { email: string, password: string } & UserProfile) => {
|
||||
return await functions().httpsCallable("createSubUser")({email, password, ...userProfile})
|
||||
if(profileType === ProfileType.parent) {
|
||||
return await functions().httpsCallable("createSubUser")({email, password, ...userProfile})
|
||||
} else {
|
||||
throw Error("Can't create sub-users as a non-parent.")
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({queryKey: ["getChildrenByParentId"]})
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import {useQuery} from "react-query";
|
||||
import {ChildProfile} from "@/hooks/firebase/types/profileTypes";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import {useAuthContext} from "@/contexts/AuthContext";
|
||||
|
||||
export const useGetChildrenByParentId = () => {
|
||||
const {user} = useAuthContext()
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["getChildrenByParentId", user?.uid],
|
||||
queryFn: async (): Promise<ChildProfile[]> => {
|
||||
try {
|
||||
const snapshot = await firestore()
|
||||
.collection("Profiles")
|
||||
.where("userType", "==", "child")
|
||||
.where("parentId", "==", user?.uid!)
|
||||
.get();
|
||||
|
||||
return snapshot.docs.map((doc) => {
|
||||
const data = doc.data();
|
||||
return {
|
||||
...data,
|
||||
birthday: data.birthday.toDate(),
|
||||
} as ChildProfile;
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error retrieving child users:", error);
|
||||
return [];
|
||||
}
|
||||
},
|
||||
enabled: !!user?.uid
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -1,11 +1,15 @@
|
||||
import {useMutation} from "react-query";
|
||||
import auth from "@react-native-firebase/auth";
|
||||
import {useUpdateUserData} from "@/hooks/firebase/useUpdateUserData";
|
||||
|
||||
export const useSignUp = () => {
|
||||
const { mutateAsync: updateUserData } = useUpdateUserData()
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["signUp"],
|
||||
mutationFn: async ({email, password}: { email: string, password: string }) => {
|
||||
await auth().createUserWithEmailAndPassword(email, password)
|
||||
await updateUserData({userType: "parent", email, password})
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user