Skip to content

Commit e536a89

Browse files
Use actual demo API
1 parent efc16eb commit e536a89

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

src/app/core/auth-module-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { OAuthModuleConfig } from 'angular-oauth2-oidc';
22

33
export const authModuleConfig: OAuthModuleConfig = {
44
resourceServer: {
5-
allowedUrls: ['http://localhost:8080'],
5+
allowedUrls: ['https://demo.identityserver.io/api'],
66
sendAccessToken: true,
77
}
88
};

src/app/feature-basics/admin1.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import { ApiService } from '../shared/api.service';
88
template: `<p class="alert alert-danger">
99
This is the <strong>⚙ ADMIN</strong> component.
1010
It will not redirect you to the login server.
11-
(API Result: '{{item | async}}')
11+
- {{ apiResponse | async }}
1212
</p>`,
1313
})
1414
export class Admin1Component implements OnInit {
15-
item: Observable<string>;
15+
apiResponse: Observable<string>;
1616

1717
constructor(private apiService: ApiService) { }
1818

1919
ngOnInit() {
20-
this.item = this.apiService.getRandomItem();
20+
this.apiResponse = this.apiService.getProtectedApiResponse();
2121
}
2222
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import { Component } from '@angular/core';
2+
import { ApiService } from '../shared/api.service';
3+
import { Observable } from 'rxjs';
24

35
@Component({
46
selector: 'app-home',
5-
template: `<p class="alert alert-primary">This is the <strong>🏠 HOME</strong> component.</p>`,
7+
template: `<p class="alert alert-primary">
8+
This is the <strong>🏠 HOME</strong> component.
9+
- {{ apiResponse | async }}
10+
</p>`,
611
})
712
export class HomeComponent {
13+
apiResponse: Observable<string>;
14+
15+
constructor(private apiService: ApiService) { }
16+
17+
ngOnInit() {
18+
this.apiResponse = this.apiService.getProtectedApiResponse();
19+
}
820
}

src/app/feature-extras/admin2.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { ApiService } from '../shared/api.service';
77
template: `<p class="alert alert-danger">
88
This is the <strong>🔧 ADMIN 2</strong> component.
99
It will redirect you to login if needed.
10-
(API Result: '{{item | async}}')
10+
- {{ apiResponse | async }}
1111
</p>`,
1212
})
1313
export class Admin2Component implements OnInit {
14-
item: Observable<string>;
14+
apiResponse: Observable<string>;
1515

1616
constructor(private apiService: ApiService) { }
1717

1818
ngOnInit() {
19-
this.item = this.apiService.getRandomItem();
19+
this.apiResponse = this.apiService.getProtectedApiResponse();
2020
}
2121
}

src/app/shared/api.service.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { Injectable } from '@angular/core';
2-
import { OAuthService } from 'angular-oauth2-oidc';
32
import { Observable, of } from 'rxjs';
3+
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
4+
import { map, catchError } from 'rxjs/operators';
45

56
@Injectable()
67
export class ApiService {
7-
constructor(private authService: OAuthService) { }
8+
constructor(private http: HttpClient) { }
89

9-
getRandomItem(): Observable<string> {
10-
if (this.authService.hasValidAccessToken()) {
11-
console.log('OK! We have a valid access token.');
12-
} else {
13-
console.error('ERROR! No valid access token for calling the API.');
14-
}
15-
16-
return of<string>('Fake API result');
10+
getProtectedApiResponse(): Observable<string> {
11+
return this.http.get<any>('https://demo.identityserver.io/api/test')
12+
.pipe(
13+
map(response => response.find(i => i.type === 'iss').value),
14+
map(iss => '☁ API Success from ' + iss),
15+
catchError((e: HttpErrorResponse) => of(`🌩 API Error: ${e.status} ${e.statusText}`)),
16+
);
1717
}
1818
}

0 commit comments

Comments
 (0)