Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import {Injectable} from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import {Observable} from 'rxjs';
import {tap} from "rxjs/operators";
import {AuthService} from "./auth.service";
@Injectable()
export class ApiInterceptor implements HttpInterceptor {
constructor(public auth: AuthService) {
}
intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
req = req.clone({
setHeaders: {
Authorization: `Bearer ${this.auth.getToken()}`
}
});
return next.handle(req).pipe(
tap(x => x, err => {
// Handle this err
console.error(`Error performing request, status code = ${err.status}`);
})
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment