1import { Injectable } from '@angular/core';
2import { HttpInterceptor, HttpEvent, HttpResponse, HttpRequest, HttpHandler } from '@angular/common/http';
3import { Observable } from 'rxjs';
4
5@Injectable()
6export class MyInterceptor implements HttpInterceptor {
7 intercept(httpRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
8 return next.handle(httpRequest);
9 }
10}
11
1// src/app/auth/auth.service.ts
2
3import { Injectable } from '@angular/core';
4import decode from 'jwt-decode';
5
6@Injectable()
7export class AuthService {
8 public getToken(): string {
9 return localStorage.getItem('token');
10 }
11
12 public isAuthenticated(): boolean {
13 // get the token
14 const token = this.getToken();
15 // return a boolean reflecting
16 // whether or not the token is expired
17 return tokenNotExpired(null, token);
18 }
19}
20
21// src/app/auth/token.interceptor.ts
22import { Injectable } from '@angular/core';
23import {
24 HttpRequest,
25 HttpHandler,
26 HttpEvent,
27 HttpInterceptor
28} from '@angular/common/http';
29import { AuthService } from './auth/auth.service';
30import { Observable } from 'rxjs/Observable';
31@Injectable()
32export class TokenInterceptor implements HttpInterceptor {
33 constructor(public auth: AuthService) {}
34 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
35
36 request = request.clone({
37 setHeaders: {
38 Authorization: `Bearer ${this.auth.getToken()}`
39 }
40 });
41 return next.handle(request);
42 }
43}
44
45// src/app/app.module.ts
46import { HTTP_INTERCEPTORS } from '@angular/common/http';
47import { TokenInterceptor } from './../auth/token.interceptor';
48
49@NgModule({ bootstrap: [AppComponent],
50 imports: [...],
51 providers: [
52 {
53 provide: HTTP_INTERCEPTORS,
54 useClass: TokenInterceptor,
55 multi: true
56 }
57 ]})
58export class AppModule {}
1// src/app/auth/token.interceptor.ts
2
3import { Injectable } from '@angular/core';
4import {
5 HttpRequest,
6 HttpHandler,
7 HttpEvent,
8 HttpInterceptor
9} from '@angular/common/http';
10import { AuthService } from './auth/auth.service';
11import { Observable } from 'rxjs/Observable';
12
13@Injectable()
14export class TokenInterceptor implements HttpInterceptor {
15
16 constructor(public auth: AuthService) {}
17
18 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
19
20 request = request.clone({
21 setHeaders: {
22 Authorization: `Bearer ${this.auth.getToken()}`
23 }
24 });
25
26 return next.handle(request);
27 }
28}
29
1// src/app/app.module.ts
2
3import { HTTP_INTERCEPTORS } from '@angular/common/http';
4import { TokenInterceptor } from './../auth/token.interceptor';
5
6@NgModule({
7 bootstrap: [AppComponent],
8 imports: [...],
9 providers: [
10 {
11 provide: HTTP_INTERCEPTORS,
12 useClass: TokenInterceptor,
13 multi: true
14 }
15 ]
16})
17export class AppModule {}
18
1
2 interface HttpInterceptor {
3 intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
4}
5
1// register the interceptor as a service
2$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
3 return {
4 // optional method
5 'request': function(config) {
6 // do something on success
7 return config;
8 },
9
10 // optional method
11 'requestError': function(rejection) {
12 // do something on error
13 if (canRecover(rejection)) {
14 return responseOrNewPromise
15 }
16 return $q.reject(rejection);
17 },
18
19
20
21 // optional method
22 'response': function(response) {
23 // do something on success
24 return response;
25 },
26
27 // optional method
28 'responseError': function(rejection) {
29 // do something on error
30 if (canRecover(rejection)) {
31 return responseOrNewPromise
32 }
33 return $q.reject(rejection);
34 }
35 };
36});
37
38$httpProvider.interceptors.push('myHttpInterceptor');
39
40
41// alternatively, register the interceptor via an anonymous factory
42$httpProvider.interceptors.push(function($q, dependency1, dependency2) {
43 return {
44 'request': function(config) {
45 // same as above
46 },
47
48 'response': function(response) {
49 // same as above
50 }
51 };
52});