forked from DuendeArchive/identity-model-oidc-client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.component.ts
More file actions
73 lines (64 loc) · 1.71 KB
/
Copy pathapp.component.ts
File metadata and controls
73 lines (64 loc) · 1.71 KB
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
import { Component, OnInit } from '@angular/core';
import { AppAuthNService, User } from './app-auth-n.service';
import { TestApiService } from './test-api.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor(public authn: AppAuthNService, public apiService: TestApiService) {
}
messages: string[] = [];
get currentUserJson() : string {
return JSON.stringify(this.currentUser, null, 2);
}
currentUser : User;
ngOnInit(): void {
this.authn.getUser().then(user => {
this.currentUser = user;
if (user){
this.addMessage("User Logged In");
}
else {
this.addMessage("User Not Logged In");
}
}).catch(err => this.addError(err));
}
clearMessages() {
while (this.messages.length) {
this.messages.pop();
}
}
addMessage(msg: string) {
this.messages.push(msg);
}
addError(msg: string | any) {
this.messages.push("Error: " + msg && msg.message);
}
public onLogin() {
this.clearMessages();
this.authn.login().catch(err => {
this.addError(err);
});
}
public onCallAPI() {
this.clearMessages();
this.apiService.callApi().then(result => {
this.addMessage("API Result: " + JSON.stringify(result));
}, err => this.addError(err));
}
public onRenewToken() {
this.clearMessages();
this.authn.renewToken()
.then(user=>{
this.currentUser = user;
this.addMessage("Silent Renew Success");
})
.catch(err => this.addError(err));
}
public onLogout() {
this.clearMessages();
this.authn.logout().catch(err => this.addError(err));
}
}