Skip to content

Instantly share code, notes, and snippets.

@wKoza
Created August 10, 2017 08:56
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 wKoza/4b95eba310ee6d6a284febe0216fbdb0 to your computer and use it in GitHub Desktop.
Save wKoza/4b95eba310ee6d6a284febe0216fbdb0 to your computer and use it in GitHub Desktop.
import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private auth: AuthService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Clone the request to add the new header.
const authReq = req.clone({headers: req.headers.set('Authorization', 'my-token')});
// Pass on the cloned request instead of the original request.
return next.handle(authReq);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment