Created
November 27, 2017 01:24
-
-
Save noroot/838558e3a38754103b6262604407d746 to your computer and use it in GitHub Desktop.
Angular 4 JWT Authorization Bearer
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 { Observable } from 'rxjs/Observable'; | |
import { HttpClient, HttpHeaders, HttpInterceptor, HttpHandler, HttpRequest, HttpEvent } from '@angular/common/http'; | |
import { Post } from './content/post'; | |
@Injectable() | |
export class CchainHttpInterceptor implements HttpInterceptor { | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
// if it is a Github API request | |
if (req.url.includes('localhost') && !req.url.includes('login')) { | |
// we need to add an OAUTH token as a header to access the Github API | |
var token = localStorage.getItem('KEY'); | |
const clone = req.clone({ | |
setHeaders: { | |
'Authorization': "Bearer " + token | |
} | |
}); | |
return next.handle(clone); | |
} | |
// if it's not a Github API request, we just handle it to the next handler | |
return next.handle(req); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment