Last active
January 3, 2020 13:53
-
-
Save nicobytes/b2ec875c28fbe876302da2f560bb21df to your computer and use it in GitHub Desktop.
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 { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http'; | |
import { Observable } from 'rxjs'; | |
@Injectable() | |
export class AuthInterceptor implements HttpInterceptor { | |
constructor() { } | |
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
request = this.addToken(request); | |
return next.handle(request); | |
} | |
private addToken(request: HttpRequest<any>) { | |
const token = '123'; | |
if (true) { | |
request = request.clone({ | |
setHeaders: { | |
token, | |
}, | |
}); | |
return request; | |
} | |
return request; | |
} | |
} | |
// | |
providers: [ | |
{ | |
provide: HTTP_INTERCEPTORS, | |
useClass: AuthInterceptor, | |
multi: true | |
} | |
], |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment