注销后权限丢失或不足错误

Raf*_*nez 8 javascript firebase ionic-framework firebase-authentication angularfire2

我在一个项目中使用 Firebase + Ionic。我的问题是登出时出现的。我订阅了onSnapshot一些集合中的几个事件。我希望每当用户注销时所有订阅都会被取消,但事实并非如此,因此每当我注销时,我都会收到来自 Firebase 的几个错误:

onSnapshot 中未捕获的错误:错误:权限缺失或不足。

这是我的代码:

我的控制器

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

我的服务商

// 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();
  }
Run Code Online (Sandbox Code Playgroud)

你能告诉我我该怎么做才能避免这些Missing or insufficient permissions错误?

先感谢您!

编辑:我如何订阅 onSnapshot 事件

控制器

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();
    });
Run Code Online (Sandbox Code Playgroud)

服务提供者

// 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);
  }
Run Code Online (Sandbox Code Playgroud)

Fra*_*len 13

由于错误消息提到onSnapshot我假设您正在访问代码中某处的 Firebase 数据库或 Cloud Firestore。

您从数据库中读取的数据配置了要求用户通过身份验证的安全规则。因此,当您注销用户时,不再满足该要求,应用将无法访问该数据,并且观察者被取消。

为防止出现此错误,请在注销用户之前移除观察者。

更新

要删除观察者/侦听器,请遵循有关分离侦听器Firestore 文档中显示的模式。首先保留对您从中获得的返回值的引用onSnapshot

var unsubscribe = this.fbDataServ.getPlacesByUserAllowed().onSnapshot(placeReceived=> {
Run Code Online (Sandbox Code Playgroud)

然后unsubscribe()在用户退出之前调用它,如下所示:

unsubscribe();
Run Code Online (Sandbox Code Playgroud)