Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing or insufficient permissions errors after logout

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);
  }
like image 589
Rafael López Martínez Avatar asked Mar 21 '18 10:03

Rafael López Martínez


1 Answers

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();
like image 112
Frank van Puffelen Avatar answered Oct 23 '22 17:10

Frank van Puffelen