Auth logic rework

This commit is contained in:
Milan Paunovic
2024-08-21 20:25:15 +02:00
parent 7500b7bdcb
commit 6b42476ef0
6 changed files with 254 additions and 200 deletions
+13 -2
View File
@@ -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"]})
}
});
}