|
2 | 2 | // configuration parameters are located at authConfig.js
|
3 | 3 | const myMSALObj = new msal.PublicClientApplication(msalConfig);
|
4 | 4 |
|
5 |
| -let username = ""; |
| 5 | +let username = ''; |
| 6 | + |
| 7 | +/** |
| 8 | + * This method adds an event callback function to the MSAL object |
| 9 | + * to handle the response from redirect flow. For more information, visit: |
| 10 | + * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/events.md |
| 11 | + */ |
| 12 | +myMSALObj.addEventCallback((event) => { |
| 13 | + if ( |
| 14 | + (event.eventType === msal.EventType.LOGIN_SUCCESS || |
| 15 | + event.eventType === msal.EventType.ACQUIRE_TOKEN_SUCCESS) && |
| 16 | + event.payload.account |
| 17 | + ) { |
| 18 | + const account = event.payload.account; |
| 19 | + myMSALObj.setActiveAccount(account); |
| 20 | + } |
6 | 21 |
|
7 |
| -function selectAccount() { |
| 22 | + if (event.eventType === msal.EventType.LOGOUT_SUCCESS) { |
| 23 | + if (myMSALObj.getAllAccounts().length > 0) { |
| 24 | + myMSALObj.setActiveAccount(myMSALObj.getAllAccounts()[0]); |
| 25 | + } |
| 26 | + } |
| 27 | +}); |
8 | 28 |
|
| 29 | +function selectAccount() { |
9 | 30 | /**
|
10 |
| - * See here for more info on account retrieval: |
| 31 | + * See here for more info on account retrieval: |
11 | 32 | * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-common/docs/Accounts.md
|
12 | 33 | */
|
13 |
| - |
14 | 34 | const currentAccounts = myMSALObj.getAllAccounts();
|
15 |
| - |
16 | 35 | if (currentAccounts === null) {
|
17 | 36 | return;
|
18 |
| - } else if (currentAccounts.length > 1) { |
| 37 | + } else if (currentAccounts.length >= 1) { |
19 | 38 | // Add choose account code here
|
20 |
| - console.warn("Multiple accounts detected."); |
21 |
| - } else if (currentAccounts.length === 1) { |
22 |
| - username = currentAccounts[0].username; |
23 |
| - showWelcomeMessage(username); |
| 39 | + username = myMSALObj.getActiveAccount().username; |
| 40 | + showWelcomeMessage(username, currentAccounts); |
24 | 41 | }
|
25 | 42 | }
|
26 | 43 |
|
27 |
| -function handleResponse(response) { |
| 44 | +async function addAnotherAccount(event) { |
| 45 | + if (event.target.innerHTML.includes('@')) { |
| 46 | + const username = event.target.innerHTML; |
| 47 | + const account = myMSALObj.getAllAccounts().find((account) => account.username === username); |
| 48 | + const activeAccount = myMSALObj.getActiveAccount(); |
| 49 | + if (account && activeAccount.homeAccountId != account.homeAccountId) { |
| 50 | + try { |
| 51 | + myMSALObj.setActiveAccount(account); |
| 52 | + let res = await myMSALObj.ssoSilent({ |
| 53 | + ...loginRequest, |
| 54 | + account: account, |
| 55 | + }); |
| 56 | + closeModal(); |
| 57 | + handleResponse(res); |
| 58 | + window.location.reload(); |
| 59 | + } catch (error) { |
| 60 | + if (error instanceof msal.InteractionRequiredAuthError) { |
| 61 | + let res = await myMSALObj.loginPopup({ |
| 62 | + ...loginRequest, |
| 63 | + prompt: 'login', |
| 64 | + }); |
| 65 | + handleResponse(res); |
| 66 | + window.location.reload(); |
| 67 | + } |
| 68 | + } |
| 69 | + } else { |
| 70 | + closeModal(); |
| 71 | + } |
| 72 | + } else { |
| 73 | + try { |
| 74 | + myMSALObj.setActiveAccount(null); |
| 75 | + const res = await myMSALObj.loginPopup({ |
| 76 | + ...loginRequest, |
| 77 | + prompt: 'login', |
| 78 | + }); |
| 79 | + handleResponse(res); |
| 80 | + closeModal(); |
| 81 | + window.location.reload(); |
| 82 | + } catch (error) { |
| 83 | + console.log(error); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
28 | 87 |
|
| 88 | +function handleResponse(response) { |
29 | 89 | /**
|
30 | 90 | * To see the full list of response object properties, visit:
|
31 | 91 | * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#response
|
32 | 92 | */
|
33 | 93 |
|
34 | 94 | if (response !== null) {
|
| 95 | + const accounts = myMSALObj.getAllAccounts(); |
35 | 96 | username = response.account.username;
|
36 |
| - showWelcomeMessage(username); |
| 97 | + showWelcomeMessage(username, accounts); |
37 | 98 | } else {
|
38 | 99 | selectAccount();
|
39 | 100 | }
|
40 | 101 | }
|
41 | 102 |
|
42 | 103 | function signIn() {
|
43 |
| - |
44 | 104 | /**
|
45 | 105 | * You can pass a custom request object below. This will override the initial configuration. For more information, visit:
|
46 | 106 | * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#request
|
47 | 107 | */
|
48 | 108 |
|
49 |
| - myMSALObj.loginPopup(loginRequest) |
| 109 | + myMSALObj |
| 110 | + .loginPopup(loginRequest) |
50 | 111 | .then(handleResponse)
|
51 |
| - .catch(error => { |
| 112 | + .catch((error) => { |
52 | 113 | console.error(error);
|
53 | 114 | });
|
54 | 115 | }
|
55 | 116 |
|
56 | 117 | function signOut() {
|
57 |
| - |
58 | 118 | /**
|
59 | 119 | * You can pass a custom request object below. This will override the initial configuration. For more information, visit:
|
60 | 120 | * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#request
|
61 | 121 | */
|
| 122 | + const account = myMSALObj.getAccountByUsername(username); |
62 | 123 | const logoutRequest = {
|
63 |
| - account: myMSALObj.getAccountByUsername(username) |
| 124 | + account: account, |
| 125 | + mainWindowRedirectUri: '/', |
64 | 126 | };
|
65 |
| - |
66 |
| - myMSALObj.logout(logoutRequest); |
| 127 | + clearStorage(account); |
| 128 | + myMSALObj.logoutPopup(logoutRequest).catch((error) => { |
| 129 | + console.log(error); |
| 130 | + }); |
67 | 131 | }
|
68 | 132 |
|
69 | 133 | function seeProfile() {
|
70 |
| - |
71 |
| - getGraphClient({ |
72 |
| - account: myMSALObj.getAccountByUsername(username), |
73 |
| - scopes: graphConfig.graphMeEndpoint.scopes, |
74 |
| - interactionType: msal.InteractionType.Popup |
75 |
| - }).api('/me').get() |
76 |
| - .then((response) => { |
77 |
| - return updateUI(response, graphConfig.graphMeEndpoint.uri); |
78 |
| - }).catch((error) => { |
79 |
| - console.log(error); |
80 |
| - }); |
| 134 | + callGraph( |
| 135 | + username, |
| 136 | + graphConfig.graphMeEndpoint.scopes, |
| 137 | + graphConfig.graphMeEndpoint.uri, |
| 138 | + msal.InteractionType.Popup, |
| 139 | + myMSALObj |
| 140 | + ); |
81 | 141 | }
|
82 | 142 |
|
83 |
| -function readMail() { |
84 |
| - |
85 |
| - getGraphClient({ |
86 |
| - account: myMSALObj.getAccountByUsername(username), |
87 |
| - scopes: graphConfig.graphMailEndpoint.scopes, |
88 |
| - interactionType: msal.InteractionType.Popup |
89 |
| - }).api('/me/messages').get() |
90 |
| - .then((response) => { |
91 |
| - return updateUI(response, graphConfig.graphMailEndpoint.uri); |
92 |
| - }).catch((error) => { |
93 |
| - console.log(error); |
94 |
| - }); |
| 143 | +function readContacts() { |
| 144 | + callGraph( |
| 145 | + username, |
| 146 | + graphConfig.graphContactsEndpoint.scopes, |
| 147 | + graphConfig.graphContactsEndpoint.uri, |
| 148 | + msal.InteractionType.Popup, |
| 149 | + myMSALObj |
| 150 | + ); |
95 | 151 | }
|
96 | 152 |
|
97 | 153 | selectAccount();
|
0 commit comments