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:
/**
* Logout button 'click' event handler
*/
onLogoutPressed(){
this.fbAuthServ.logout().then(() => {
this.navCtrl.setRoot(LoginPage);
});
}
// 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!
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();
});
// 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);
}
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With