I am having issues with my Angular 6 Interceptor. I have migrated the code from Angular 5.x to 6.0. and rxjs 5.x to 6.x. When I try to create a new user account, the request seems to be called twice. I know this because I log the error to the console. I have noticed that I get this duplication when I try to get the Id token from firebase auth, See code snippet below.
intercept (request: HttpRequest<any>, next: HttpHandler): observable<HttpEvent<any>> {
return this.auth.getIdToken().pipe(
mergeMap((token: any) => {
if (token) {
request = request.clone({ setHeaders: { Authorization: `Bearer ${token}` } });
}
return next.handle(request).pipe(
tap((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
}
}, (err: any) => {
if (err instanceof HttpErrorResponse) {
if ((err.status === 401) || (err.status === 403)) {
this.router.navigate([WellknownRoutesConstants.SIGN_IN], { queryParams: { returnUrl: this.router.routerState.snapshot.url }});
}
}
})
);
}));
}
getIdToken (): Observable<any> {
return ngFireAuth.authState.pipe(
tap((user) => {
if (user) {
return observableOf(user.getIdToken());
} else {
return observableOf;
}
}),
catchError(observableOf)
);
}