Created
October 8, 2018 16:45
-
-
Save rexar1988/ef37951b3e987fb6d74d3252fb5331de to your computer and use it in GitHub Desktop.
Angular 2+: Interceptor
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 {ApiAuthService} from './api-auth.service'; | |
import {HttpRequest, HttpEvent, HttpHandler, HttpInterceptor} from '@angular/common/http'; | |
import {Injectable} from '@angular/core'; | |
import {Observable} from 'rxjs/Observable'; | |
@Injectable() | |
export class AuthInterceptor implements HttpInterceptor { | |
constructor(private authService: ApiAuthService) {} | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
const copiedAuthRequest = req.clone({ | |
setHeaders: { | |
Authorization: `Bearer ${this.authService.accessToken}` | |
} | |
}); | |
return next.handle(copiedAuthRequest); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment