Daruul
Daruul

Reputation: 45

Firebase Auth: can't delete Firestore documents in promise returned from currentUser.delete()

I am using firebase authentication and angularfirestore in my app, and I came across a strange problem. I want to delete a user document in firestore after I delete the user in firebase auth as shown below, but the firestore portion doesnt work.

Edit 2: In the code, this.currentUser is not the same as angularFireAuth.auth.currentUser. It is a local variable I created separately.

return this.angularFireAuth.auth.currentUser.delete().then(() => {
            this.angularFirestore.collection("userData").doc(this.currentUser.email).delete().then(() => {
                this.currentUser = null;
                this.currentUserSubject$.next(this.currentUser);
                this.setAuthenticationDetails({authType: null, rememberMe: false});
                this.storageService.setRememberedUser(null);
            });
        })

However, if I were delete the document from firestore first, and delete the user from firebase auth afterwards, it works.

return this.angularFirestore.collection("userData").doc(this.currentUser.email).delete().then(() => {
            return this.angularFireAuth.auth.currentUser.delete().then(() => {
                this.currentUser = null;
                this.currentUserSubject$.next(this.currentUser);
                this.setAuthenticationDetails({authType: null, rememberMe: false});
                this.storageService.setRememberedUser(null);
            });
        })

Anyone knows why this is happening? They both return promises so i expected the behaviour to be the same.

Edit 1: added catch, but nothing's being printed

return this.angularFireAuth.auth.currentUser.delete().then(() => {
            return this.angularFirestore.collection("userData").doc(this.currentUser.email).delete().then(() => {
                this.currentUser = null;
                this.currentUserSubject$.next(this.currentUser);
                this.setAuthenticationDetails({authType: null, rememberMe: false});
                this.storageService.setRememberedUser(null);
            }, (error) => {
                console.log(error);
            });
        }, (error) => {
            console.log(error);
        })

Edit 3: Security Rules

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

Upvotes: 1

Views: 239

Answers (2)

Ashely Shu
Ashely Shu

Reputation: 13

I think what happened is that after you do auth.currentUser.delete(), this.currentUser also gets cleared (if it holds auth.currentUser)

So you are just deleting doc("") which didn't fail. But there is no such document, so you won't see anything.

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317710

What you're observing makes sense to me.

If you have security rules set up on Firestore that require an authenticated user in order to delete the documents, it makes sense that the documents would fail to delete after removing the user account (as the user is not longer authenticated). You will definitely want to do everything you can while authenticated before signing out or deleting the user account.

You should add error checking to your code to find out if anything went wrong with the document delete. Right now, you don't have any catch callbacks on the promise chain, so you'd never know if something went wrong. I suspect you'll see a "permission denied" type of error on the document delete due to security rules rejecting the request.

Upvotes: 1

Related Questions