0

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!

jrs
  • 135
  • 1
  • 16
  • 2
    "I'm having a few issues" Please edit your question to show what issues you are having, including the exact error message and entire stack trace for each. – Frank van Puffelen Jun 20 '22 at 00:25
  • Thanks for your reply, fair point! I was tired and frustrated when I wrote this post initially. I have updated this post with a little bit more detail with my issue and the code I have written. Cheers! @FrankvanPuffelen – jrs Jun 20 '22 at 21:46
  • 1
    You can check this [post](https://stackoverflow.com/a/59601773/16531380) that discussed your similar issue/error. – RJC Jun 21 '22 at 01:37

1 Answers1

2

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 })))
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks Frank - that was really helpful. Although, it seems that my Firestore data still exists. I logged it out and got: Error: [firestore/permission-denied] The caller does not have permission to execute the specified operation. Here are how my Firestore rules look... rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if request.auth != null } } } – jrs Jun 21 '22 at 22:17
  • 1
    That seems like a different problem, but the explanation is the same: if your rules require that the user is signed in in order to be allowed to delete data, you can't delete data anymore after deleting the user. In general, the pattern I follow upon deletion is to reverse the order of when I registered/created the user, so first delete the data, and only then delete the user. – Frank van Puffelen Jun 21 '22 at 23:53