jani_r
jani_r

Reputation: 721

Angular interceptor with AngularFire

Does anyone know is it possible to intercept the requests made to firebase cloudstore with AngularFire library ?

I would like to implement a functionality that shows a toast message in the angular interceptor when adding/updating/deleting items from the cloudstore

I am using AngularFire library and AngularFirestore.

My interceptor works fine for other http requests but not when when using the library only using the angular builtin HttpClient library. I can see in the network console that xhr request are made when adding/updating/deleting items from cloudfirestore.

How can I intercept the requests made from AngularFirestore library ?

I am using @angular/fire 5.1.2 and angular 7.2

Thanks, Jani

Upvotes: 7

Views: 2037

Answers (2)

Nemanja Andric
Nemanja Andric

Reputation: 665

To handle response errors for Firestore operations in Angular, you can use the catchError operator in the observable chain after calling a Firestore operation. For example:

this.afs.collection('users').doc('userId').set({ name: 'John' })
  .pipe(
    catchError(error => {
      // Handle the error here
      console.error(error);
      return of(null);
    })
  )
  .subscribe();

Upvotes: 1

Sebastian Vischer
Sebastian Vischer

Reputation: 1310

The Angular interceptor only works for http request made with the http client. AngularFire uses the Firebase SDK for Firestore operations, which does not use the http client from Angular. So the requests can not be intercepted.

You could send a message inside the function which calls the operation. Or use background functions from Firebase Cloud Function.

Upvotes: 6

Related Questions