Skip to content

Instantly share code, notes, and snippets.

@rexar1988
Created October 8, 2018 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rexar1988/ef37951b3e987fb6d74d3252fb5331de to your computer and use it in GitHub Desktop.
Save rexar1988/ef37951b3e987fb6d74d3252fb5331de to your computer and use it in GitHub Desktop.
Angular 2+: Interceptor
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