Skip to content

Instantly share code, notes, and snippets.

@mogeta
Created March 20, 2021 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mogeta/fd6be49e1c0372df051f8128463950c2 to your computer and use it in GitHub Desktop.
Save mogeta/fd6be49e1c0372df051f8128463950c2 to your computer and use it in GitHub Desktop.
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