/
app.component.ts
75 lines (61 loc) · 1.64 KB
/
app.component.ts
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { OAuthService, NullValidationHandler } from 'angular-oauth2-oidc';
import { authConfig, DiscoveryDocumentConfig } from './auth.config';
@Component({
selector: 'app-root',
template: `
<h1 *ngIf="!claims">
Hi!
</h1>
<h1 *ngIf="claims">
Hi, {{claims.given_name}}!
</h1>
<h2 *ngIf="claims">Your Claims:</h2>
<pre *ngIf="claims">
{{claims | json}}
</pre>
<br />
<div *ngIf="!claims">
<button (click)="login()">Login</button>
</div>
<div *ngIf="claims">
<button (click)="logout()">Logout</button>
<button (click)="getMessage()">API Call</button>
<div *ngIf="message">
Response:
{{message | json}}
</div>
</div>
`,
styles: []
})
export class AppComponent {
constructor(private http: HttpClient, private oauthService: OAuthService) {
this.configure();
this.oauthService.tryLoginImplicitFlow();
}
message: string;
public getMessage() {
this.http.get("https://localhost:5001/api/values", { responseType: 'text' })
.subscribe(r => {
this.message = r
console.log("message: ", this.message);
});
}
public login() {
this.oauthService.initLoginFlow();
}
public logout() {
this.oauthService.logOut();
}
public get claims() {
let claims = this.oauthService.getIdentityClaims();
return claims;
}
private configure() {
this.oauthService.configure(authConfig);
this.oauthService.tokenValidationHandler = new NullValidationHandler();
this.oauthService.loadDiscoveryDocument(DiscoveryDocumentConfig.url);
}
}