Z
Z
zlodiak2018-02-03 11:52:00
Angular
zlodiak, 2018-02-03 11:52:00

Why is the interceptor not working?

I am using angular 5.1.3. I'm trying to write an interceptor that adds a header to any http request
Authorization: '12345'
My interceptor:

import { Injectable } from '@angular/core';
import {
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpInterceptor
} from '@angular/common/http';
import { AuthService } from './auth.service';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class TokenInterceptor implements HttpInterceptor {

  constructor(public auth: AuthService) {}

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {    
    request = request.clone({
      setHeaders: {
        Authorization: '12345'
      }
    });
    return next.handle(request);
  }

}

I include it in app.module.ts like this:
...
...
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { TokenInterceptor } from './Modules/Core/Services/auth.interceptor';

@NgModule({
....
...
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: TokenInterceptor,
      multi: true
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

As a result, even before sending the request in the browser console, I get the following error:
compiler.js:19541 Uncaught Error: Provider parse errors:
Cannot instantiate cyclic dependency! InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./[email protected]:-1
at NgModuleProviderAnalyzer.parse (compiler.js:19541)

Please help me send these headers

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question