1

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;
    }
  }
}

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi, i added the catch callbacks, but in the console nothing is being printed (which is very strange, since I'd expect some error if the request is not going through?). Also, I don't have any security rules that require users to be authenticated
Even if you have not written security rules on your own, it might have security rules like below created on project creation. So be mindful about that. Daruul makes perfect sense in his comment and we can not perform operation if user is not logged if security rules exits. firebase.google.com/docs/firestore/security/get-started
My security rule allows for all operations so can't be that
Please edit the question to show your security rules.
If you don't get some sort of result after a document write, then you should file a bug with angularfire with your specific steps to reproduce.
|
0

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.

1 Comment

Thats my bad, I didn't make it clear that this.currentUser is a local variable that isn't affected by auth.currentUser(). I've tested it with console.log and this.currentUser isn't null, hence why I asked this. Really confused

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.