Skip to content

Commit bada91c

Browse files
committed
add account picker
1 parent 4b7b713 commit bada91c

File tree

5 files changed

+299
-100
lines changed

5 files changed

+299
-100
lines changed

2-Authorization-I/1-call-graph/App/authPopup.js

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,99 @@ const myMSALObj = new msal.PublicClientApplication(msalConfig);
44

55
let username = '';
66

7+
myMSALObj.addEventCallback((event) => {
8+
if (
9+
(event.eventType === 'msal:loginSuccess' || event.eventType === 'msal:acquireTokenSuccess') &&
10+
event.payload.account
11+
) {
12+
const account = event.payload.account;
13+
myMSALObj.setActiveAccount(account);
14+
}
15+
16+
if (event.eventType === 'msal:logoutSuccess') {
17+
if (myMSALObj.getAllAccounts().length > 0) {
18+
myMSALObj.setActiveAccount(myMSALObj.getAllAccounts()[0]);
19+
}
20+
}
21+
});
22+
723
function selectAccount() {
824
/**
925
* See here for more info on account retrieval:
1026
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-common/docs/Accounts.md
1127
*/
12-
1328
const currentAccounts = myMSALObj.getAllAccounts();
14-
1529
if (currentAccounts === null) {
1630
return;
1731
} else if (currentAccounts.length > 1) {
1832
// Add choose account code here
1933
console.warn('Multiple accounts detected.');
34+
username = myMSALObj.getActiveAccount().username;
35+
showWelcomeMessage(username, currentAccounts);
2036
} else if (currentAccounts.length === 1) {
21-
username = currentAccounts[0].username;
22-
showWelcomeMessage(username);
37+
username = myMSALObj.getActiveAccount().username;
38+
showWelcomeMessage(username, currentAccounts);
2339
}
2440
}
2541

26-
function handleResponse(response) {
42+
async function addAnotherAccount(event) {
43+
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(event.target.innerHTML)) {
44+
const username = event.target.innerHTML;
45+
const account = myMSALObj.getAllAccounts().find((account) => account.username === username);
46+
const activeAccount = myMSALObj.getActiveAccount();
47+
if (account && activeAccount.homeAccountId != account.homeAccountId) {
48+
try {
49+
myMSALObj.setActiveAccount(account);
50+
let res = await myMSALObj.ssoSilent({
51+
...loginRequest,
52+
account: account,
53+
});
54+
handleResponse(res);
55+
window.location.reload();
56+
} catch (error) {
57+
if (error instanceof msal.InteractionRequiredAuthError) {
58+
let res = await instance.loginPopup({
59+
...loginRequest,
60+
prompt: 'login',
61+
});
62+
handleResponse(res);
63+
window.location.reload();
64+
}
65+
}
66+
} else {
67+
closeModal();
68+
}
69+
} else {
70+
try {
71+
myMSALObj.setActiveAccount(null);
72+
const res = await myMSALObj.loginPopup({
73+
...loginRequest,
74+
prompt: 'login',
75+
});
76+
handleResponse(res);
77+
closeModal();
78+
} catch (error) {
79+
console.log(error);
80+
}
81+
}
82+
}
2783

84+
function handleResponse(response) {
2885
/**
2986
* To see the full list of response object properties, visit:
3087
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#response
3188
*/
3289

3390
if (response !== null) {
91+
const accounts = myMSALObj.getAllAccounts();
3492
username = response.account.username;
35-
showWelcomeMessage(username);
93+
showWelcomeMessage(username, accounts);
3694
} else {
3795
selectAccount();
3896
}
3997
}
4098

4199
function signIn() {
42-
43100
/**
44101
* You can pass a custom request object below. This will override the initial configuration. For more information, visit:
45102
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#request
@@ -110,7 +167,7 @@ function seeProfile() {
110167
console.log(error);
111168
});
112169
} else {
113-
console.log(error)
170+
console.log(error);
114171
}
115172
});
116173
}
@@ -129,7 +186,7 @@ function readContacts() {
129186
return handleClaimsChallenge(account, response, graphConfig.graphContactsEndpoint.uri);
130187
})
131188
.then((response) => {
132-
if (response && response.error === 'claims_challenge_occurred') throw response.error;
189+
if (response && response.error === 'claims_challenge_occurred') throw response.error;
133190
return updateUI(response, graphConfig.graphContactsEndpoint.uri);
134191
})
135192
.catch((error) => {

2-Authorization-I/1-call-graph/App/authRedirect.js

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ const myMSALObj = new msal.PublicClientApplication(msalConfig);
44

55
let username = '';
66

7+
8+
myMSALObj.addEventCallback((event) => {
9+
if (
10+
(event.eventType === 'msal:loginSuccess' || event.eventType === 'msal:acquireTokenSuccess') &&
11+
event.payload.account
12+
) {
13+
const account = event.payload.account;
14+
myMSALObj.setActiveAccount(account);
15+
}
16+
17+
if (event.eventType === 'msal:logoutSuccess') {
18+
if (myMSALObj.getAllAccounts().length > 0) {
19+
myMSALObj.setActiveAccount(myMSALObj.getAllAccounts()[0]);
20+
}
21+
}
22+
});
23+
724
/**
825
* A promise handler needs to be registered for handling the
926
* response returned from redirect flow. For more information, visit:
@@ -29,16 +46,57 @@ function selectAccount() {
2946
} else if (currentAccounts.length > 1) {
3047
// Add your account choosing logic here
3148
console.warn('Multiple accounts detected.');
49+
username = myMSALObj.getActiveAccount().username;
50+
showWelcomeMessage(username, currentAccounts);
3251
} else if (currentAccounts.length === 1) {
33-
username = currentAccounts[0].username;
34-
showWelcomeMessage(username);
52+
username = myMSALObj.getActiveAccount().username;
53+
showWelcomeMessage(username, currentAccounts);
54+
}
55+
}
56+
57+
async function addAnotherAccount(event) {
58+
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(event.target.innerHTML)) {
59+
const username = event.target.innerHTML;
60+
const account = myMSALObj.getAllAccounts().find((account) => account.username === username);
61+
const activeAccount = myMSALObj.getActiveAccount();
62+
if (account && activeAccount.homeAccountId != account.homeAccountId) {
63+
try {
64+
myMSALObj.setActiveAccount(account);
65+
let res = await myMSALObj.ssoSilent({
66+
...loginRequest,
67+
account: account,
68+
});
69+
handleResponse(res);
70+
window.location.reload();
71+
} catch (error) {
72+
if (error instanceof msal.InteractionRequiredAuthError) {
73+
await instance.loginRedirect({
74+
...loginRequest,
75+
prompt: 'login',
76+
});
77+
}
78+
}
79+
} else {
80+
closeModal();
81+
}
82+
} else {
83+
try {
84+
myMSALObj.setActiveAccount(null);
85+
await myMSALObj.loginRedirect({
86+
...loginRequest,
87+
prompt: 'login',
88+
});
89+
} catch (error) {
90+
console.log(error);
91+
}
3592
}
3693
}
3794

3895
function handleResponse(response) {
3996
if (response !== null) {
97+
const accounts = myMSALObj.getAllAccounts();
4098
username = response.account.username;
41-
showWelcomeMessage(username);
99+
showWelcomeMessage(username, accounts);
42100
} else {
43101
selectAccount();
44102
}

0 commit comments

Comments
 (0)