Skip to content

Instantly share code, notes, and snippets.

@noroot
Created November 27, 2017 01:24
Show Gist options
  • Save noroot/838558e3a38754103b6262604407d746 to your computer and use it in GitHub Desktop.
Save noroot/838558e3a38754103b6262604407d746 to your computer and use it in GitHub Desktop.
Angular 4 JWT Authorization Bearer
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