@@ -4,42 +4,99 @@ const myMSALObj = new msal.PublicClientApplication(msalConfig);
4
4
5
5
let username = '' ;
6
6
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
+
7
23
function selectAccount ( ) {
8
24
/**
9
25
* See here for more info on account retrieval:
10
26
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-common/docs/Accounts.md
11
27
*/
12
-
13
28
const currentAccounts = myMSALObj . getAllAccounts ( ) ;
14
-
15
29
if ( currentAccounts === null ) {
16
30
return ;
17
31
} else if ( currentAccounts . length > 1 ) {
18
32
// Add choose account code here
19
33
console . warn ( 'Multiple accounts detected.' ) ;
34
+ username = myMSALObj . getActiveAccount ( ) . username ;
35
+ showWelcomeMessage ( username , currentAccounts ) ;
20
36
} else if ( currentAccounts . length === 1 ) {
21
- username = currentAccounts [ 0 ] . username ;
22
- showWelcomeMessage ( username ) ;
37
+ username = myMSALObj . getActiveAccount ( ) . username ;
38
+ showWelcomeMessage ( username , currentAccounts ) ;
23
39
}
24
40
}
25
41
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
+ }
27
83
84
+ function handleResponse ( response ) {
28
85
/**
29
86
* To see the full list of response object properties, visit:
30
87
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#response
31
88
*/
32
89
33
90
if ( response !== null ) {
91
+ const accounts = myMSALObj . getAllAccounts ( ) ;
34
92
username = response . account . username ;
35
- showWelcomeMessage ( username ) ;
93
+ showWelcomeMessage ( username , accounts ) ;
36
94
} else {
37
95
selectAccount ( ) ;
38
96
}
39
97
}
40
98
41
99
function signIn ( ) {
42
-
43
100
/**
44
101
* You can pass a custom request object below. This will override the initial configuration. For more information, visit:
45
102
* 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() {
110
167
console . log ( error ) ;
111
168
} ) ;
112
169
} else {
113
- console . log ( error )
170
+ console . log ( error ) ;
114
171
}
115
172
} ) ;
116
173
}
@@ -129,7 +186,7 @@ function readContacts() {
129
186
return handleClaimsChallenge ( account , response , graphConfig . graphContactsEndpoint . uri ) ;
130
187
} )
131
188
. then ( ( response ) => {
132
- if ( response && response . error === 'claims_challenge_occurred' ) throw response . error ;
189
+ if ( response && response . error === 'claims_challenge_occurred' ) throw response . error ;
133
190
return updateUI ( response , graphConfig . graphContactsEndpoint . uri ) ;
134
191
} )
135
192
. catch ( ( error ) => {
0 commit comments