I am facing an issue where I need to intercept every request to api, check the status code, and display a message or redirect to a specific component. However, I keep encountering the following error:
main.js:1580 TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.at subscribeTo (vendor.js:179688)at subscribeToResult(vendor.js:179824) at MergeMapSubscriber._innerSub (vendor.js:175271) at mergeMapSubscriber._tryNext (vendor.js:175265) at MergeMapSubscriber._next (vendor.js:175248) at MergeMapSubscriber.next (vendor.js:170316) at Observable._subscribe (vendor.js:172287) at Observable._trySubscribe (vendor.js:169772) at Observable.subscribe (vendor.js:169758) at MergeMapOperator.call (vendor.js:175233)
This is my AuthInterceptor:
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { tap } from 'rxjs/operators';
import { CommonService } from '../common.service';
import { Router } from '@angular/router';
import { AuthenticationService } from './authentication.service';
@Injectable({
providedIn: 'root'
})
export class AuthInterceptorService implements HttpInterceptor {
constructor(
private common: CommonService,
private router: Router,
private auth: AuthenticationService
) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.headers.get('No-Auth') === 'True') {
return next.handle(req.clone());
}
// To attach header on every request
if (localStorage.getItem('currentUser') != null) {
const clonedreq = req.clone({
headers: req.headers.set('Authorization', 'Bearer ' + localStorage.getItem('currentUser'))
});
return next.handle(clonedreq).pipe(tap(
succ => { },
err => {
if (err.status === 401) {
this.router.navigateByUrl('/login');
} else if (err.status === 403) {
this.router.navigateByUrl('/Forbidden');
} else if (err.status === 400) {
this.router.navigateByUrl('/error404');
}
}
));
} else {
this.router.navigateByUrl('/login');
}
}
}
I have been unable to identify the line or block of code that is causing this error, as it is not mentioned in the error message. The project compiles without any issues, but this error occurs on almost every page whenever a request is sent to the WEB Api.