-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
75 lines (67 loc) · 2.25 KB
/
index.html
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
<html>
<head>
<script src="https://alcdn.msauth.net/browser/2.28.3/js/msal-browser.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@microsoft/microsoft-graph-client/lib/graph-js-sdk.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/@microsoft/microsoft-graph-client/lib/graph-client-msalBrowserAuthProvider.js"></script>
<script src="./env.js"></script>
</head>
<body>
<h1>Hello Microsoft 365</h1>
<div id="auth"></div>
<pre></pre>
<script>
(() => {
const msalConfig = {
auth: {
clientId: appId
}
};
const msalClient = new msal.PublicClientApplication(msalConfig);
function render() {
msalClient
.handleRedirectPromise()
.then(response => {
const accounts = msalClient.getAllAccounts();
if (accounts.length === 0) {
document.querySelector('#auth').innerHTML = '<button>Login</button>';
document.querySelector('#auth button').addEventListener('click', login);
document.querySelector('pre').innerHTML = '';
}
else {
document.querySelector('#auth').innerHTML = '<button>Logout</button>';
document.querySelector('#auth button').addEventListener('click', logout);
const graphClient = getGraphClient(accounts[0]);
loadUserProfile(graphClient);
}
});
}
function login(e) {
e.preventDefault();
msalClient.loginRedirect();
}
function logout(e) {
e.preventDefault();
msalClient.logoutRedirect();
}
function getGraphClient(account) {
const authProvider = new MSGraphAuthCodeMSALBrowserAuthProvider.AuthCodeMSALBrowserAuthenticationProvider(msalClient, {
account,
scopes: ['user.read'],
interactionType: msal.InteractionType.Redirect,
});
return MicrosoftGraph.Client.initWithMiddleware({ authProvider });
}
function loadUserProfile(graphClient) {
graphClient
.api('/me')
.get()
.then(res => {
document.querySelector('pre').innerHTML = JSON.stringify(res, null, 2);
});
}
render();
})();
</script>
</body>
</html>