-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_esm.html
85 lines (75 loc) · 2.53 KB
/
index_esm.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
76
77
78
79
80
81
82
83
84
85
<html>
<head>
<script type="importmap">
{
"imports": {
"tslib": "https://unpkg.com/tslib/tslib.es6.js",
"@azure/msal-common": "https://unpkg.com/@azure/msal-common/dist/index.js",
"@azure/msal-browser": "https://unpkg.com/@azure/msal-browser/dist/index.js",
"@microsoft/microsoft-graph-client": "https://unpkg.com/@microsoft/microsoft-graph-client/lib/es/src/index.js"
}
}
</script>
</head>
<body>
<h1>Hello Microsoft 365</h1>
<div id="auth"></div>
<pre></pre>
<script type="module">
import { PublicClientApplication, InteractionType } from '@azure/msal-browser';
import { Client } from '@microsoft/microsoft-graph-client';
import { AuthCodeMSALBrowserAuthenticationProvider } from 'https://unpkg.com/@microsoft/microsoft-graph-client/lib/es/src/authentication/msal-browser/AuthCodeMSALBrowserAuthenticationProvider.js';
import { appId } from './env_esm.js';
const msalConfig = {
auth: {
clientId: appId
}
};
const msalClient = new PublicClientApplication(msalConfig);
let graphClient = null;
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);
graphClient = getGraphClient(accounts[0]);
loadUserProfile();
}
});
}
function login(e) {
e.preventDefault();
msalClient.loginRedirect();
}
function logout(e) {
e.preventDefault();
msalClient.logoutRedirect();
}
function getGraphClient(account) {
const authProvider = new AuthCodeMSALBrowserAuthenticationProvider(msalClient, {
account,
scopes: ['user.read'],
interactionType: InteractionType.Redirect,
});
return Client.initWithMiddleware({ authProvider });
}
function loadUserProfile() {
graphClient
.api('/me')
.get()
.then(res => {
document.querySelector('pre').innerHTML = JSON.stringify(res, null, 2);
});
}
render();
</script>
</body>
</html>