I'm trying to delete a user from Auth and Firestore on button click in one go using react-native-firebase. My issue at the moment is that it is not deleting the document 'user' from Firestore. BUT, it does look like I am deleting the user from Auth.
Also when I press the button that calls the deleteUserFunc I receive the error:
"null is not an object (evaluating '(0, _auth.default)().currentUser.uid')"
(UID has a valid id on page load)
const [_, setUser] = useContext(UserContext);
const uid = auth().currentUser.uid;
const deleteUserFunc = async () => {
await deleteAccount();
}
async function deleteAccount() {
auth().currentUser.delete().then(() => {
db.collection('users')
.doc(user.uid)
.delete()
}).catch((error) => {
console.log(error, "error")
}).finally(() => setUser((state) => ({ ...state, isLoggedIn: false })))
}
How can I delete from the Firestore for this user, as well as improve the code that I have written.
Any help is appreciated - thank you for your time!
My guess is that you set your user
variable to point to auth.currentUser
, which becomes null
when you delete the current user. So you'll need to either reverse the operations (first delete the document, then the user), or you can capture the UID in a local variable before deleting the user.
The latter would look like:
async function deleteAccount() {
const uid = auth().currentUser.uid; // 👈
auth().currentUser.delete().then(() => {
db.collection('users')
.doc(uid) // 👈
.delete()
}).catch((error) => {
console.log(error, "error")
}).finally(() => setUser((state) => ({ ...state, isLoggedIn: false })))
}