Missing or insufficient permissions errors after logout
Asked Answered
V

2

13

I'm using Firebase + Ionic in a project. my problem comes when loging out. I am subscribed to several onSnapshot events in some collections. I expect all subscriptions to be dismissed whenever the a user is logged out, but it is not like that, so whenever I logged out I receive several errors comming from Firebase:

Uncaught Error in onSnapshot: Error: Missing or insufficient permissions.

Here my code:

My controller

/**
 * Logout button 'click' event handler
 */
onLogoutPressed(){
  this.fbAuthServ.logout().then(() => {
    this.navCtrl.setRoot(LoginPage);
  });
} 

My service provider

// Firebase Modules
import { AngularFireAuth } from 'angularfire2/auth';

constructor(private afAuth: AngularFireAuth, private utils: UtilsProvider){}

...

  /**
    * Firebase Logout the current user
   */
  async logout(){
    this.afAuth.auth.signOut();
  }

Could you please tell me what should I do to avoid those Missing or insufficient permissions errors??

Thank you in advance!

EDIT: How I get subscribe to onSnapshot events

Controller

ionViewDidEnter(){

    this.fbDataServ.getPlacesByUserAllowed().onSnapshot(placeReceived=> {
      this.placesByUserAllowed = placeReceived.docs.map(placeSnapshot => {
        return this.utils.mapPlaceSnapshot(placeSnapshot )
      });
      this._concatPlaces();

      //Dismiss loading whenever we have data available
      this.utils.dismissLoading();
    });

Service Provider

// Firebase
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';

constructor(private afs: AngularFirestore, private fbAuthServ: FirebaseAuthServiceProvider, private utils: UtilsProvider) { }

placesCollection: AngularFirestoreCollection<PlaceModel> = this.afs.collection("places");



  /**
   * Gets the collection of places where the user is 'allowed'
   */
  public getPlacesByUserAllowed(){
    return this.placesCollection.ref
    .where('users.' + this.fbAuthServ.getCurrentUser().uid + '.allowed', '==', true);
  }
Volnak answered 21/3, 2018 at 10:57 Comment(0)
J
16

Since the error message mentions onSnapshot I assume you're accessing the Firebase Database or Cloud Firestore somewhere in your code.

The data that you're reading from the database has security rules configured that require that the user is authenticated. So when you log the user out, that requirement is no longer met, the app loses access to that data, and the observer is canceled.

To prevent this error from appearing, remove the observer before logging the user out.

Update

To remove the observer/listener follow the patterns shown in the Firestore documentation on detaching a listener. First keep a reference to the return value you get from onSnapshot.

var unsubscribe = this.fbDataServ.getPlacesByUserAllowed().onSnapshot(placeReceived=> {

Then call that unsubscribe() just before signing the user out like this:

unsubscribe();
Jural answered 21/3, 2018 at 14:39 Comment(4)
Hi Frank! Thank you, I know about the rules I have in Firestore. My question is about how to do this 'cleanUp' process before logging out. How can I 'remove the observer' ?? thanks for your helpBalanchine
That depends on how you add them, which unfortunately you didn't share the code for. Can you update your question to include the code that adds the observer?Jural
You are right!! Sorry for that... I've just edit my post with the code where I add one of the observersBalanchine
Works great!! Thank you Frank!!Balanchine
P
0

Here's the updated link and code as of August 30th 2021: https://firebase.google.com/docs/firestore/query-data/listen#handle_listen_errors

Here's the code:

let listener = db.collection("cities").addSnapshotListener { querySnapshot, error in
    // ...
}    
        
// Stop listening to changes
listener()
Physiology answered 6/1, 2021 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.