sajesh Nambiar
sajesh Nambiar

Reputation: 699

HttpInterceptor next.handle is not a function

I am upgrading my application from Angular 5 to Angular 8. I have created a HttpInterceptor but I am getting this error:

ERROR next.handle is not a function (core.js).

I have checked that HttpClientModule is only loaded once so I am not sure what is causing this issue.

Can you please help me to fix this?

import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, throwError as observableThrowError } from 'rxjs';
import { catchError } from 'rxjs/operators';

import { AppService } from './services/app.service';

@Injectable(
  {
    providedIn: 'root'
  })
export class AuthHttpInterceptor implements HttpInterceptor {
  constructor(private authService: AppService, private router: Router) { }
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    //const jwt = this.authService.getAuthToken();
    const jwt = localStorage.getItem('jwt');
    const authRequest = req.clone({
      setHeaders: { authorization: `Bearer ${jwt}` }, withCredentials: true
    });
    return next.handle(req).pipe(
      catchError((err, caught) => {
        if (err.status === 401) {
          this.router.navigate(['/login'], {
            queryParams: { redirectUrl: this.router.routerState.snapshot.url }
          });
        }

        return new Observable<HttpEvent<any>>();
      })
    );
  }
}

Upvotes: 0

Views: 743

Answers (1)

sajesh Nambiar
sajesh Nambiar

Reputation: 699

Found the issue, there was an injection of HttpBackend on the app.module i removed it and it started working. It might be helpful for others because we spent almost half day on this.

Upvotes: 1

Related Questions