-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpInterceptor.ts
More file actions
49 lines (42 loc) · 1.37 KB
/
Copy pathhttpInterceptor.ts
File metadata and controls
49 lines (42 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {Injectable} from '@angular/core';
import {HttpRequest, HttpHandler, HttpEvent, HttpInterceptor} from '@angular/common/http';
import {AuthService} from './auth/auth.service';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(public auth: AuthService) {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.auth.getToken()}`
}
});
return next.handle(request);
/* This one for manipulating Error response */
/* return next.handle(req).do((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do stuff with response if you want
}
}, (err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
this.auth.collectFailedRequest(request);
}
}
});*/
}
}
import { HTTP_INTERCEPTORS } from '@angular/common/http';
@NgModule({
bootstrap: [AppComponent],
imports: [],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
}
]
})
export class AppModule {}