Skip to content

Instantly share code, notes, and snippets.

@danielcrisp
Created July 25, 2017 13:39
Show Gist options
  • Save danielcrisp/fe3ad9e23c9d3641aeb63af06400897d to your computer and use it in GitHub Desktop.
Save danielcrisp/fe3ad9e23c9d3641aeb63af06400897d to your computer and use it in GitHub Desktop.
TokenInterceptor - Async HTTP Interceptors with Angular 4
intercept (request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return this.auth.getUser()
.mergeMap((user: User) => {
if (user) {
// clone and modify the request
request = request.clone({
setHeaders: {
Authorization: `Bearer ${user.access_token}`
}
});
}
return next.handle(request);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment