Skip to content

Commit fabd309

Browse files
authored
Merge branch 'Azure-Samples:main' into main
2 parents 46e3256 + d8a3ebb commit fabd309

30 files changed

Lines changed: 40910 additions & 611 deletions

File tree

1-Authentication/0-sign-in-vanillajs/App/package-lock.json

Lines changed: 5017 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

1-Authentication/0-sign-in-vanillajs/App/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
"author": "",
1111
"license": "ISC",
1212
"dependencies": {
13-
"@azure/msal-browser": "^2.37.0",
14-
"express": "^4.18.2",
13+
"@azure/msal-browser": "^3.16.0",
14+
"express": "^4.19.2",
1515
"morgan": "^1.10.0"
1616
},
1717
"devDependencies": {
18-
"jest": "^29.5.0",
19-
"jest-environment-jsdom": "^29.5.0",
20-
"supertest": "^6.3.3"
18+
"jest": "^29.7.0",
19+
"jest-environment-jsdom": "^29.7.0",
20+
"supertest": "^7.0.0"
2121
}
2222
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
const msalConfig = {
77
auth: {
88
clientId: 'Enter_the_Application_Id_Here', // This is the ONLY mandatory field that you need to supply.
9-
authority: 'https://Enter_the_Tenant_Subdomain_Here.ciamlogin.com/', // Replace the placeholder with your tenant subdomain
10-
redirectUri: '/', // You must register this URI on Azure Portal/App Registration. Defaults to window.location.href e.g. http://localhost:3000/
9+
authority: 'https://Enter_the_Tenant_Subdomain_Here.ciamlogin.com/', // Replace the placeholder with your tenant subdomain
10+
redirectUri: 'http://localhost:3000/redirect', // You must register this URI on Azure Portal/App Registration. Defaults to window.location.href e.g. http://localhost:3000/
1111
navigateToLoginRequestUrl: true, // If "true", will navigate back to the original request location before processing the auth code response.
1212
},
1313
cache: {
@@ -65,4 +65,4 @@ if (typeof exports !== 'undefined') {
6565
msalConfig: msalConfig,
6666
loginRequest: loginRequest,
6767
};
68-
}
68+
}
Lines changed: 104 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,104 @@
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

Comments
 (0)