Created
March 20, 2021 03:29
-
-
Save mogeta/fd6be49e1c0372df051f8128463950c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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