1

I am not getting any errors, and am able to set the authToken value from my login component with a valid token (that I have verified with Postman). But I do not get any Authorization parameter added to my request header.

 entryComponents: [
    ConfirmDialogTargetComponent
  ],
  providers: [
    AuthInterceptor,
    {
      provide: HTTP_INTERCEPTORS,
      useClass: AuthInterceptor,
      multi: true     
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

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

@Injectable()
export class AuthInterceptor {

    authToken: string = '';

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        const authReq = req.clone({ headers: req.headers.set('Authorization', 'Bearer ' + this.authToken) });
        return next.handle(authReq);
    }
}
Jota.Toledo
  • 25,115
  • 9
  • 55
  • 68
Austin Harris
  • 4,792
  • 6
  • 24
  • 37

1 Answers1

0

Remove the first AuthInterceptor:

 entryComponents: [
    ConfirmDialogTargetComponent
  ],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: AuthInterceptor,
      multi: true     
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
pixelbits
  • 50,631
  • 15
  • 93
  • 132
  • unfortunately I tried that and it does not help. It's there because the login component imports it so that it can set the authToken value inside the AuthInterceptor service. If I remove the first instance of AuthInterceptor in the providers array then the login component errors. – Austin Harris Nov 08 '17 at 04:52