简体   繁体   中英

firestore unsubscribe finished event

I am subscribed to a document in firestore, before I logout I want to unsubscribe otherwise it will throw an error since I am using firestore security rules and when the user is logged out and I didn't unsubscribe the access will be denied and it will throw an error.

I tried the code below but still not working I still get errors from firestore security rules, so I introduced setInterval but it is taking too much time (in my test I have gone till 5s to be able to avoid the error)

Do you have a better approach to avoid this issue, I mean is there an event that is fired once the unsubscription is done (I want to avoid setInterval with long time)?

var unsubscribe = db.collection('notifications').doc('docID').onSnapshot(
          function(doc) {
            // Do things
          })

function logout () {
  unsubscribe(
    setTimeout(function() {
      firebase.auth().signOut().then(() => {
        // Redirect to home page
      });
    }, 600)
  )
};

As far as I know, unsubscribing is synchronous. So when the unsubscribe() is done, you can just log out:

unsubscribe();
firebase.auth().signOut().then(() => {
    // Redirect to home page
});

If you're still getting security errors after this, you might still have another listener attached for the user. If that doesn't seem to be the case, please edit your question to include the minimal, complete/standalone code that we can run to reproduce the problem . Alternatively, you can (also) set up a minimal reproduction of the problem on a site like jsbin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM