最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Missing or insufficient permissions errors after logout - Stack Overflow

programmeradmin1浏览0评论

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

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);
  }
Share Improve this question edited Mar 22, 2018 at 13:56 Frank van Puffelen 599k85 gold badges888 silver badges858 bronze badges asked Mar 21, 2018 at 10:57 Rafael López MartínezRafael López Martínez 2,2652 gold badges16 silver badges25 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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();

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()
发布评论

评论列表(0)

  1. 暂无评论