Created
November 21, 2017 15:50
-
-
Save elvinmeza/c705f270a0e63c407d7dab1a3c6814ee to your computer and use it in GitHub Desktop.
Angular Http Interceptor
This file contains hidden or 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 {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http'; | |
import { Observable } from 'rxjs/Observable'; | |
@Injectable() | |
export class AuthInterceptor implements HttpInterceptor { | |
// constructor( INJECT YOUR AUTH SERVICE HERE ) | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
console.log('New request intercepted: ', req); | |
// get your authorization header from the service | |
const authHeader = 'Bearer BLAHBLAHBLAHBLAH'; | |
const authReq = req.clone({setHeaders: {'Authorization': authHeader}}); | |
console.log('Modified request: ', authReq); | |
return next.handle(authReq); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment