-
Notifications
You must be signed in to change notification settings - Fork 0
/
HybridSPA.js
141 lines (121 loc) · 5.08 KB
/
HybridSPA.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const msalInstance = new msal.PublicClientApplication({
auth: {
clientId: "35dc5034-9b65-4a5d-ad81-73cca468c1e0",
redirectUri: "https://localhost:44320/auth/client-redirect",
authority: "https://login.microsoftonline.com/common"
}
})
// Select DOM elements to work with
const welcomeDiv = document.getElementById("welcomeMessage");
const signInButton = document.getElementById("signIn");
const signOutButton = document.getElementById('signOut');
const cardDiv = document.getElementById("card-div");
const mailButton = document.getElementById("readMail");
const profileButton = document.getElementById("seeProfile");
const profileDiv = document.getElementById("profile-div");
// Add here the endpoints for MS Graph API services you would like to use.
const graphConfig = {
graphMeEndpoint: "https://graph.microsoft.com/v1.0/me",
graphMailEndpoint: "https://graph.microsoft.com/v1.0/me/messages"
};
function updateUI(data, endpoint) {
console.log('Graph API responded at: ' + new Date().toString());
if (endpoint === graphConfig.graphMeEndpoint) {
profileDiv.innerHTML = '';
const title = document.createElement('p');
title.innerHTML = "<strong>Title: </strong>" + data.jobTitle;
console.log(data.jobTitle);
const email = document.createElement('p');
email.innerHTML = "<strong>Mail: </strong>" + data.mail;
console.log(data.mail);
const phone = document.createElement('p');
phone.innerHTML = "<strong>Phone: </strong>" + data.businessPhones[0];
console.log(data.businessPhones[0]);
const address = document.createElement('p');
address.innerHTML = "<strong>Location: </strong>" + data.officeLocation;
console.log(data.officeLocation);
profileDiv.appendChild(title);
profileDiv.appendChild(email);
profileDiv.appendChild(phone);
profileDiv.appendChild(address);
} else if (endpoint === graphConfig.graphMailEndpoint) {
if (data.value.length < 1) {
alert("Your mailbox is empty!")
} else {
console.log('Displaying Emails for the signed in user.');
const tabList = document.getElementById("list-tab");
tabList.innerHTML = ''; // clear tabList at each readMail call
const tabContent = document.getElementById("nav-tabContent");
data.value.map((d, i) => {
// Keeping it simple
if (i < 10) {
const listItem = document.createElement("a");
listItem.setAttribute("class", "list-group-item list-group-item-action")
listItem.setAttribute("id", "list" + i + "list")
listItem.setAttribute("data-toggle", "list")
listItem.setAttribute("href", "#list" + i)
listItem.setAttribute("role", "tab")
listItem.setAttribute("aria-controls", i)
listItem.innerHTML = d.subject;
tabList.appendChild(listItem)
const contentItem = document.createElement("div");
contentItem.setAttribute("class", "tab-pane fade")
contentItem.setAttribute("id", "list" + i)
contentItem.setAttribute("role", "tabpanel")
contentItem.setAttribute("aria-labelledby", "list" + i + "list")
contentItem.innerHTML = "<strong> from: " + d.from.emailAddress.address + "</strong>";
tabContent.appendChild(contentItem);
}
});
}
}
}
// Helper function to call MS Graph API endpoint
// using authorization bearer token scheme
function callMSGraph(endpoint, token, callback) {
const headers = new Headers();
const bearer = `Bearer ${token}`;
headers.append("Authorization", bearer);
const options = {
method: "GET",
headers: headers
};
console.log('request made to Graph API at: ' + new Date().toString());
fetch(endpoint, options)
.then(response => response.json())
.then(response => callback(response, endpoint))
.then(result => {
console.log('Successfully Fetched Data from Graph API:', result);
})
.catch(error => console.log(error))
}
///get Token
function getTokenPopup(spaCode) {
var code = spaCode;
const scopes = ["user.read"];
console.log('MSAL: acquireTokenByCode hybrid parameters present');
var authResult = msalInstance.acquireTokenByCode({
code,
scopes
})
console.log(authResult);
return authResult
}
//Read Email
function readMail(spaCode) {
getTokenPopup(spaCode)
.then(response => {
callMSGraph(graphConfig.graphMailEndpoint, response.accessToken, updateUI);
}).catch(error => {
console.log(error);
});
}
//See Profile
function seeProfile(spaCode) {
getTokenPopup(spaCode)
.then(response => {
callMSGraph(graphConfig.graphMeEndpoint, response.accessToken, updateUI);
}).catch(error => {
console.log(error);
});
}