Skip to content

Commit f2e0b8f

Browse files
committed
HttpClient, Interceptor
1 parent 16bbff2 commit f2e0b8f

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

src/app/app.module.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { AppComponent } from './app.component';
66
import {MatButtonModule, MatIconModule, MatInputModule} from '@angular/material';
77
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
88
import {FlightModule} from './flight/flight.module';
9+
import {HTTP_INTERCEPTORS, HttpClientModule} from '@angular/common/http';
10+
import {AuthInterceptorService} from './core/auth-interceptor.service';
911

1012
@NgModule({
1113
declarations: [
@@ -18,9 +20,16 @@ import {FlightModule} from './flight/flight.module';
1820
MatInputModule,
1921
MatIconModule,
2022
BrowserAnimationsModule,
21-
FlightModule
23+
FlightModule,
24+
HttpClientModule
25+
],
26+
providers: [
27+
{
28+
provide: HTTP_INTERCEPTORS,
29+
useClass: AuthInterceptorService,
30+
multi: true
31+
}
2232
],
23-
providers: [],
2433
bootstrap: [AppComponent]
2534
})
2635
export class AppModule { }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { AuthInterceptorService } from './auth-interceptor.service';
4+
5+
describe('AuthInterceptorService', () => {
6+
beforeEach(() => TestBed.configureTestingModule({}));
7+
8+
it('should be created', () => {
9+
const service: AuthInterceptorService = TestBed.get(AuthInterceptorService);
10+
expect(service).toBeTruthy();
11+
});
12+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {Injectable} from '@angular/core';
2+
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
3+
import {Observable} from 'rxjs';
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class AuthInterceptorService implements HttpInterceptor {
9+
10+
constructor() {
11+
}
12+
13+
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
14+
const token = '123456abcde';
15+
if (token) {
16+
const authReq = req.clone({headers: req.headers.set('Authorization', token)});
17+
return next.handle(authReq);
18+
} else {
19+
return next.handle(req);
20+
}
21+
}
22+
}

src/app/flight/flight.component.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import {Component, OnInit} from '@angular/core';
22
import {FlightService} from './flight.service';
33

44
@Component({
@@ -9,10 +9,12 @@ import {FlightService} from './flight.service';
99
export class FlightComponent implements OnInit {
1010
flights: any;
1111

12-
constructor(private flightService: FlightService) { }
12+
constructor(private flightService: FlightService) {
13+
}
1314

1415
ngOnInit() {
15-
this.flights = this.flightService.listFlights();
16+
this.flightService.listFlights()
17+
.subscribe(data => console.log(data), e => console.log(e), () => console.log('complete'));
1618
}
1719

1820
}

src/app/flight/flight.service.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
import { Injectable } from '@angular/core';
1+
import {Injectable} from '@angular/core';
2+
import {HttpClient, HttpHeaders} from '@angular/common/http';
3+
import {Observable} from 'rxjs';
24

35
@Injectable({
46
providedIn: 'root'
57
})
68
export class FlightService {
79

8-
constructor() { }
10+
constructor(private http: HttpClient) {
11+
}
12+
13+
listFlights(): Observable<any> {
14+
const httpOptions = {
15+
headers: new HttpHeaders({
16+
'Content-Type': 'application/json',
17+
'Authorization': 'my-auth-token'
18+
})
19+
};
920

10-
listFlights() {
11-
return '["Flights"]';
21+
return this.http.get(`/someUrl`, httpOptions);
1222
}
1323
}

0 commit comments

Comments
 (0)