Adding the HttpInterceptor in the AppModule

It goes without saying that our AuthInterceptor class will only work if we properly configure it within our AppModule class. To do that, open the app.module.shared.ts file and add the following (new code highlighted):

[...]import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';import { RouterModule } from '@angular/router';import { AuthService } from './services/auth.service';import { AuthInterceptor } from './services/auth.interceptor';[...]providers: [    AuthService,    {        provide: HTTP_INTERCEPTORS,        useClass: AuthInterceptor,        multi: true    }][...]

If we did everything correctly, now the token should be automatically added to each HTTP request. We can easily test it from the server ...

Get ASP.NET Core 2 and Angular 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.