/
login.ts
41 lines (37 loc) · 1.32 KB
/
login.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
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
import { PublicClientApplication } from "@azure/msal-browser";
Office.onReady(async () => {
const pca = new PublicClientApplication({
auth: {
clientId: 'YOUR APP ID HERE',
authority: 'https://login.microsoftonline.com/common',
redirectUri: `${window.location.origin}/login/login.html` // Must be registered as "spa" type.
},
cache: {
cacheLocation: 'localStorage' // Needed to avoid a "login required" error.
}
});
await pca.initialize();
try {
// handleRedirectPromise should be invoked on every page load.
const response = await pca.handleRedirectPromise();
if (response) {
Office.context.ui.messageParent(JSON.stringify({ status: 'success', token: response.accessToken, userName: response.account.username}));
} else {
// A problem occurred, so invoke login.
await pca.loginRedirect({
scopes: ['user.read', 'files.read.all']
});
}
} catch (error) {
const errorData = {
errorMessage: error.errorCode,
message: error.errorMessage,
errorCode: error.stack
};
Office.context.ui.messageParent(JSON.stringify({ status: 'failure', result: errorData }));
}
});