Skip to content

Commit 810e2e9

Browse files
committed
use createPublicClientApplication() pattern
1 parent 609c322 commit 810e2e9

4 files changed

Lines changed: 42 additions & 46 deletions

File tree

1-Authentication/0-sign-in-vanillajs/App/public/authPopup.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
// Create the main myMSALObj instance
22
// 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-
});
3+
let myMSALObj;
4+
5+
msal.PublicClientApplication.createPublicClientApplication(msalConfig)
6+
.then((obj) => {
7+
myMSALObj = obj;
8+
})
9+
.catch((error) => {
10+
console.error("Error creating MSAL PublicClientApplication:", error);
1611
});
1712

1813
let username = "";

1-Authentication/0-sign-in-vanillajs/App/public/authRedirect.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
// Create the main myMSALObj instance
22
// 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)
3+
msal.PublicClientApplication.createPublicClientApplication(msalConfig)
4+
.then((obj) => {
5+
myMSALObj = obj;
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+
})
1317
.catch((error) => {
14-
console.error(error);
18+
console.error("Error creating MSAL PublicClientApplication:", error);
1519
});
16-
});
1720

1821
let username = "";
1922

2-Authorization/0-call-api-vanillajs/SPA/public/authPopup.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
// Create the main myMSALObj instance
22
// configuration parameters are located at authConfig.js
3-
const myMSALObj = new msal.PublicClientApplication(msalConfig);
3+
let myMSALObj;
44

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-
});
5+
msal.PublicClientApplication.createPublicClientApplication(msalConfig)
6+
.then((obj) => {
7+
myMSALObj = obj;
8+
})
9+
.catch((error) => {
10+
console.error("Error creating MSAL PublicClientApplication:", error);
1611
});
1712

1813
let username = '';

2-Authorization/0-call-api-vanillajs/SPA/public/authRedirect.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
// Create the main myMSALObj instance
22
// 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()
3+
msal.PublicClientApplication.createPublicClientApplication(msalConfig)
4+
.then((obj) => {
5+
myMSALObj = obj;
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()
1212
.then(handleResponse)
1313
.catch((error) => {
1414
console.error(error);
1515
});
16+
})
17+
.catch((error) => {
18+
console.error("Error creating MSAL PublicClientApplication:", error);
1619
});
1720

1821
let username = '';

0 commit comments

Comments
 (0)