Skip to content

Instantly share code, notes, and snippets.

@amilamen
Created June 11, 2021 12:40
Show Gist options
  • Save amilamen/48351c6f2647065f1b0a2802a1471082 to your computer and use it in GitHub Desktop.
Save amilamen/48351c6f2647065f1b0a2802a1471082 to your computer and use it in GitHub Desktop.
intercept(request: HttpRequest<any>, next: HttpHandler) {
return from(this.storageService.getToken()).pipe(
switchMap((token) => {
if (token) {
request = request.clone({
headers: request.headers.set("Authorization", "Bearer " + token),
});
}
request = request.clone({
headers: request.headers.set("Accept", "application/json"),
});
return next.handle(request).pipe(
map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
}
return event;
}),
catchError((error: HttpErrorResponse) => {
return throwError(error);
})
);
})
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment