For my Angular 9 application, I have created a custom interceptor called AuthorizationHeaderInterceptor
:
@Injectable()
export class AuthorizationHeaderInterceptor implements HttpInterceptor {
constructor(private authenticationService: AuthenticationService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return from(this.authenticationService.getAuthenticationHeader()).pipe(header => {
request = request.clone({
setHeaders: {
Authorization: `${header}`
}
});
return next.handle(request);
});
}
}
The method
authenticationService.getAuthenticationHeader()
retrieves a header value as a Promise<string>
, which is then added to the request.
To convert the Promise into an Observable
, I utilize from
and pipe
on the resulting Observable
.
Question: Is it necessary to include map
after applying pipe
? And furthermore, is this the correct approach for implementing this interceptor?