I'm new to Angular and I've just built an interceptor. According to multiple tutorials you have to include the HTTP_INTERCEPTORS
in the app.module
like so:
providers: [{ provide: HTTP_INTERCEPTORS, useClass: MyInterceptor, multi: true }]
I was wondering what that multi: true
attribute means/does and whether it can be omitted or not.
I've read the angular.io guide about this attribute. They explain it as following:
I don't understand this part:
Note the multi: true option. This required setting tells Angular that HTTP_INTERCEPTORS is a token for a multiprovider that injects an array of values, rather than a single value.
This sheds some light on the concept but I don't really understand yet when an interceptor is injecting multiple values and when it isn't. For example, my own interceptor is only changing the headers. Does this mean its injecting only a single value?
This is my interceptor
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { LoginService } from '../Services/login.service';
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
constructor(private loginService:LoginService){}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// add authorization header with jwt token if available
console.log("ik zit nu in de interceptor");
let currentUser = this.loginService.getToken();
if (currentUser !=="") {
request = request.clone({
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': `Bearer ${currentUser}`
})
});
}
return next.handle(request);
}
}
This is the provide list of app.module
providers: [
{ provide: APP_BASE_HREF, useValue: '/' },
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
{ provide: 'AUTH_URL', useValue: 'http://localhost:8080/auth' },
{ provide: 'API_URL', useValue: 'http://localhost:8080/api' },
{ provide: 'HEADERS', useValue: new HttpHeaders({'Content-Type': 'application/json'}) },
LoginGuard,
LoginService,
UserService,
MessageService
],