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 ...