Files
cally/hooks/firebase/useDeleteUser.ts
Milan Paunovic 70db8bdc0b New calendar
2024-12-15 16:29:34 +01:00

32 lines
1.0 KiB
TypeScript

import {useAuthContext} from "@/contexts/AuthContext";
import {useMutation} from "@tanstack/react-query";
import firestore from "@react-native-firebase/firestore";
import auth, {FirebaseAuthTypes} from "@react-native-firebase/auth";
export const useDeleteUser = () => {
const {user: currentUser} = useAuthContext();
return useMutation({
mutationKey: ["deleteUser"],
mutationFn: async ({customUser}: { customUser?: FirebaseAuthTypes.User }) => {
const user = currentUser ?? customUser;
if (user) {
try {
await firestore()
.collection("Profiles")
.doc(user.uid)
.delete();
await auth().currentUser?.delete();
await auth().signOut();
console.log("User deleted and signed out successfully");
} catch (e) {
console.error("Error deleting user:", e);
}
}
},
});
};