File tree Expand file tree Collapse file tree 5 files changed +30
-18
lines changed Expand file tree Collapse file tree 5 files changed +30
-18
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import { OAuthModuleConfig } from 'angular-oauth2-oidc';
2
2
3
3
export const authModuleConfig : OAuthModuleConfig = {
4
4
resourceServer : {
5
- allowedUrls : [ 'http ://localhost:8080 ' ] ,
5
+ allowedUrls : [ 'https ://demo.identityserver.io/api ' ] ,
6
6
sendAccessToken : true ,
7
7
}
8
8
} ;
Original file line number Diff line number Diff line change @@ -8,15 +8,15 @@ import { ApiService } from '../shared/api.service';
8
8
template : `<p class="alert alert-danger">
9
9
This is the <strong>⚙ ADMIN</strong> component.
10
10
It will not redirect you to the login server.
11
- (API Result: '{{item | async}}')
11
+ - {{ apiResponse | async }}
12
12
</p>` ,
13
13
} )
14
14
export class Admin1Component implements OnInit {
15
- item : Observable < string > ;
15
+ apiResponse : Observable < string > ;
16
16
17
17
constructor ( private apiService : ApiService ) { }
18
18
19
19
ngOnInit ( ) {
20
- this . item = this . apiService . getRandomItem ( ) ;
20
+ this . apiResponse = this . apiService . getProtectedApiResponse ( ) ;
21
21
}
22
22
}
Original file line number Diff line number Diff line change 1
1
import { Component } from '@angular/core' ;
2
+ import { ApiService } from '../shared/api.service' ;
3
+ import { Observable } from 'rxjs' ;
2
4
3
5
@Component ( {
4
6
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>` ,
6
11
} )
7
12
export class HomeComponent {
13
+ apiResponse : Observable < string > ;
14
+
15
+ constructor ( private apiService : ApiService ) { }
16
+
17
+ ngOnInit ( ) {
18
+ this . apiResponse = this . apiService . getProtectedApiResponse ( ) ;
19
+ }
8
20
}
Original file line number Diff line number Diff line change @@ -7,15 +7,15 @@ import { ApiService } from '../shared/api.service';
7
7
template : `<p class="alert alert-danger">
8
8
This is the <strong>🔧 ADMIN 2</strong> component.
9
9
It will redirect you to login if needed.
10
- (API Result: '{{item | async}}')
10
+ - {{ apiResponse | async }}
11
11
</p>` ,
12
12
} )
13
13
export class Admin2Component implements OnInit {
14
- item : Observable < string > ;
14
+ apiResponse : Observable < string > ;
15
15
16
16
constructor ( private apiService : ApiService ) { }
17
17
18
18
ngOnInit ( ) {
19
- this . item = this . apiService . getRandomItem ( ) ;
19
+ this . apiResponse = this . apiService . getProtectedApiResponse ( ) ;
20
20
}
21
21
}
Original file line number Diff line number Diff line change 1
1
import { Injectable } from '@angular/core' ;
2
- import { OAuthService } from 'angular-oauth2-oidc' ;
3
2
import { Observable , of } from 'rxjs' ;
3
+ import { HttpClient , HttpErrorResponse } from '@angular/common/http' ;
4
+ import { map , catchError } from 'rxjs/operators' ;
4
5
5
6
@Injectable ( )
6
7
export class ApiService {
7
- constructor ( private authService : OAuthService ) { }
8
+ constructor ( private http : HttpClient ) { }
8
9
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
+ ) ;
17
17
}
18
18
}
You can’t perform that action at this time.
0 commit comments