1- // Create the main myMSALObj instance
2- // configuration parameters are located at authConfig.js
3- const myMSALObj = new msal . PublicClientApplication ( msalConfig ) ;
4-
5- let username = "" ;
6-
7- function selectAccount ( ) {
8-
9- /**
10- * See here for more info on account retrieval:
11- * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-common/docs/Accounts.md
12- */
13-
14- const currentAccounts = myMSALObj . getAllAccounts ( ) ;
15-
16- if ( ! currentAccounts || currentAccounts . length < 1 ) {
17- return ;
18- } else if ( currentAccounts . length > 1 ) {
19- // Add your account choosing logic here
20- console . warn ( "Multiple accounts detected." ) ;
21- } else if ( currentAccounts . length === 1 ) {
22- username = currentAccounts [ 0 ] . username
23- welcomeUser ( currentAccounts [ 0 ] . username ) ;
24- updateTable ( currentAccounts [ 0 ] ) ;
25- }
26- }
27-
28- function handleResponse ( response ) {
29-
30- /**
31- * To see the full list of response object properties, visit:
32- * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#response
33- */
34-
35- if ( response !== null ) {
36- username = response . account . username
37- welcomeUser ( username ) ;
38- updateTable ( response . account ) ;
39- } else {
40- selectAccount ( ) ;
41-
42- /**
43- * If you already have a session that exists with the authentication server, you can use the ssoSilent() API
44- * to make request for tokens without interaction, by providing a "login_hint" property. To try this, comment the
45- * line above and uncomment the section below.
46- */
47-
48- // myMSALObj.ssoSilent(silentRequest).
49- // then((response) => {
50- // welcomeUser(response.account.username);
51- // updateTable(response.account);
52- // }).catch(error => {
53- // console.error("Silent Error: " + error);
54- // if (error instanceof msal.InteractionRequiredAuthError) {
55- // signIn();
56- // }
57- // });
58- }
59- }
60-
61- function signIn ( ) {
62-
63- /**
64- * You can pass a custom request object below. This will override the initial configuration. For more information, visit:
65- * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#request
66- */
67-
68- myMSALObj . loginPopup ( loginRequest )
69- . then ( handleResponse )
70- . catch ( error => {
71- console . error ( error ) ;
72- } ) ;
73- }
74-
75- function signOut ( ) {
76-
77- /**
78- * You can pass a custom request object below. This will override the initial configuration. For more information, visit:
79- * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#request
80- */
81-
82- // Choose which account to logout from by passing a username.
83- const logoutRequest = {
84- account : myMSALObj . getAccountByUsername ( username ) ,
85- mainWindowRedirectUri : '/signout'
86- } ;
87-
88- myMSALObj . logoutPopup ( logoutRequest ) ;
89- }
90-
91- selectAccount ( ) ;
1+ // Create the main myMSALObj instance
2+ // configuration parameters are located at authConfig.js
3+ const myMSALObj = new msal . PublicClientApplication ( msalConfig ) ;
4+
5+ myMSALObj . initialize ( ) . then ( ( ) => {
6+ /**
7+ * A promise handler needs to be registered for handling the
8+ * response returned from redirect flow. For more information, visit:
9+ * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/initialization.md#redirect-apis
10+ */
11+ myMSALObj . handleRedirectPromise ( )
12+ . then ( handleResponse )
13+ . catch ( ( error ) => {
14+ console . error ( error ) ;
15+ } ) ;
16+ } ) ;
17+
18+ let username = "" ;
19+
20+ function selectAccount ( ) {
21+
22+ /**
23+ * See here for more info on account retrieval:
24+ * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-common/docs/Accounts.md
25+ */
26+
27+ const currentAccounts = myMSALObj . getAllAccounts ( ) ;
28+
29+ if ( ! currentAccounts || currentAccounts . length < 1 ) {
30+ return ;
31+ } else if ( currentAccounts . length > 1 ) {
32+ // Add your account choosing logic here
33+ console . warn ( "Multiple accounts detected." ) ;
34+ } else if ( currentAccounts . length === 1 ) {
35+ username = currentAccounts [ 0 ] . username
36+ welcomeUser ( currentAccounts [ 0 ] . username ) ;
37+ updateTable ( currentAccounts [ 0 ] ) ;
38+ }
39+ }
40+
41+ function handleResponse ( response ) {
42+
43+ /**
44+ * To see the full list of response object properties, visit:
45+ * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#response
46+ */
47+
48+ if ( response !== null ) {
49+ username = response . account . username
50+ welcomeUser ( username ) ;
51+ updateTable ( response . account ) ;
52+ } else {
53+ selectAccount ( ) ;
54+
55+ /**
56+ * If you already have a session that exists with the authentication server, you can use the ssoSilent() API
57+ * to make request for tokens without interaction, by providing a "login_hint" property. To try this, comment the
58+ * line above and uncomment the section below.
59+ */
60+
61+ // myMSALObj.ssoSilent(silentRequest).
62+ // then((response) => {
63+ // welcomeUser(response.account.username);
64+ // updateTable(response.account);
65+ // }).catch(error => {
66+ // console.error("Silent Error: " + error);
67+ // if (error instanceof msal.InteractionRequiredAuthError) {
68+ // signIn();
69+ // }
70+ // });
71+ }
72+ }
73+
74+ function signIn ( ) {
75+
76+ /**
77+ * You can pass a custom request object below. This will override the initial configuration. For more information, visit:
78+ * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#request
79+ */
80+ loginRequest . redirectUri = "/redirect" ;
81+ myMSALObj . loginPopup ( loginRequest )
82+ . then ( handleResponse )
83+ . catch ( error => {
84+ console . error ( error ) ;
85+ } ) ;
86+ }
87+
88+ function signOut ( ) {
89+
90+ /**
91+ * You can pass a custom request object below. This will override the initial configuration. For more information, visit:
92+ * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#request
93+ */
94+
95+ // Choose which account to logout from by passing a username.
96+ const logoutRequest = {
97+ account : myMSALObj . getAccount ( { username : username } ) ,
98+ mainWindowRedirectUri : '/signout'
99+ } ;
100+
101+ myMSALObj . logoutPopup ( logoutRequest ) ;
102+ }
103+
104+ selectAccount ( ) ;
0 commit comments