-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.component.ts
76 lines (67 loc) · 1.77 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
76
import { Component, OnInit } from '@angular/core';
import { MsalService } from "@azure/msal-angular";
import {BroadcastService} from "@azure/msal-angular";
import {HttpClient} from "@angular/common/http";
import { HttpHeaders } from '@angular/common/http';
import { GraphService } from "./services/graph.service";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'angular-spa-oauth2-implicit-flow';
loggedin: boolean = false;
username: string = "";
profile: string = "";
constructor(
private broadcastService: BroadcastService,
private authService: MsalService,
private graphService: GraphService)
{
}
ngOnInit(): void
{
this.broadcastService.subscribe("msal:loginFailure", (payload) =>
{
console.log("login failure " + JSON.stringify(payload));
this.loggedin = false;
});
this.broadcastService.subscribe("msal:loginSuccess", (payload) =>
{
console.log("login success " + JSON.stringify(payload));
this.loggedin = true;
});
if (!this.authService.getUser())
{
this.loggedin = false;
}
else
{
this.loggedin = true;
this.username = this.authService.getUser().name;
}
}
public onLogin(): void
{
// specify your scopes to consent
this.authService.loginRedirect(["User.Read"]);
}
public onLogout(): void
{
this.authService.logout();
}
public onGetUserProfile() : void
{
this.profile = "Loading..."
this.graphService.getUserProfile()
.then(result => result.subscribe(profile =>
{
this.profile = profile;
}))
.catch(reason =>
{
this.profile = reason;
});
}
}