Skip to content

Commit 0550b1f

Browse files
JavaScript sample update (#155)
* react-spa-code-update * remove-files * update-vanillajs-app * update-changes * update-README * update-README * update-README * update-config * update-auth-config * udate-sample-to-fix-plus-upgrade --------- Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent f115d4a commit 0550b1f

File tree

10 files changed

+782
-4251
lines changed

10 files changed

+782
-4251
lines changed

vanillajs-spa/App/package-lock.json

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

vanillajs-spa/App/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ms-identity-javascript-c1s1",
33
"version": "1.0.0",
4-
"description": "Vanilla JavaScript single-page application (SPA) using MSAL.js to authenticate users using Microsoft Entra ID",
4+
"description": "JavaScript single-page application (SPA) using MSAL.js to authenticate users using Microsoft Entra ID",
55
"main": "server.js",
66
"scripts": {
77
"start": "node server.js",
@@ -28,12 +28,14 @@
2828
},
2929
"homepage": "https://github.com/Azure-Samples/ms-identity-javascript-tutorial#readme",
3030
"dependencies": {
31-
"@azure/msal-browser": "^2.37.0",
32-
"express": "^4.18.2",
31+
"express": "^4.20.0",
3332
"morgan": "^1.10.0"
3433
},
3534
"devDependencies": {
36-
"jest": "^27.0.6",
35+
"@azure/msal-browser": "^4.0.0",
36+
"@types/jest": "^29.5.0",
37+
"@types/express": "^4.17.3",
38+
"@types/morgan": "^1.9.0",
3739
"nodemon": "^2.0.20",
3840
"supertest": "^6.1.4"
3941
},

vanillajs-spa/App/public/auth.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Browser check variables
2+
// If you support IE, our recommendation is that you sign-in using Redirect APIs
3+
// If you as a developer are testing using Edge InPrivate mode, please add "isEdge" to the if check
4+
const ua = window.navigator.userAgent;
5+
const msie = ua.indexOf("MSIE ");
6+
const msie11 = ua.indexOf("Trident/");
7+
const msedge = ua.indexOf("Edge/");
8+
const isIE = msie > 0 || msie11 > 0;
9+
const isEdge = msedge > 0;
10+
11+
let signInType;
12+
let accountId = "";
13+
14+
// Create the main myMSALObj instance
15+
// configuration parameters are located at authConfig.js
16+
const myMSALObj = new msal.PublicClientApplication(msalConfig);
17+
18+
myMSALObj.initialize().then(() => {
19+
// Redirect: once login is successful and redirects with tokens, call Graph API
20+
myMSALObj.handleRedirectPromise().then(handleResponse).catch(err => {
21+
console.error(err);
22+
});
23+
})
24+
25+
function selectAccount() {
26+
27+
/**
28+
* See here for more info on account retrieval:
29+
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-common/docs/Accounts.md
30+
*/
31+
32+
const currentAccounts = myMSALObj.getAllAccounts();
33+
34+
if (!currentAccounts) {
35+
return;
36+
} else if (currentAccounts.length > 1) {
37+
// Add your account choosing logic here
38+
console.warn("Multiple accounts detected.");
39+
} else if (currentAccounts.length === 1) {
40+
username = currentAccounts[0].username
41+
showWelcomeMessage(currentAccounts[0].username);
42+
updateTable(currentAccounts[0]);
43+
}
44+
}
45+
46+
function handleResponse(resp) {
47+
if (resp !== null) {
48+
accountId = resp.account.homeAccountId;
49+
myMSALObj.setActiveAccount(resp.account);
50+
showWelcomeMessage(resp.account);
51+
} else {
52+
selectAccount();
53+
}
54+
}
55+
56+
async function signIn(method) {
57+
signInType = isIE ? "redirect" : method;
58+
if (signInType === "popup") {
59+
return myMSALObj.loginPopup({
60+
...loginRequest,
61+
redirectUri: "/redirect"
62+
}).then(handleResponse).catch(function (error) {
63+
console.log(error);
64+
});
65+
} else if (signInType === "redirect") {
66+
return myMSALObj.loginRedirect(loginRequest)
67+
}
68+
}
69+
70+
function signOut(interactionType) {
71+
const logoutRequest = {
72+
account: myMSALObj.getAccountByHomeId(accountId)
73+
};
74+
75+
if (interactionType === "popup") {
76+
myMSALObj.logoutPopup(logoutRequest).then(() => {
77+
window.location.reload();
78+
});
79+
} else {
80+
myMSALObj.logoutRedirect(logoutRequest);
81+
}
82+
}
83+
84+
// This function can be removed if you do not need to support IE
85+
async function getTokenRedirect(request, account) {
86+
return await myMSALObj.acquireTokenSilent(request).catch(async (error) => {
87+
console.log("silent token acquisition fails.");
88+
if (error instanceof msal.InteractionRequiredAuthError) {
89+
// fallback to interaction when silent call fails
90+
console.log("acquiring token using redirect");
91+
myMSALObj.acquireTokenRedirect(request);
92+
} else {
93+
console.error(error);
94+
}
95+
});
96+
}

vanillajs-spa/App/public/authConfig.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
const msalConfig = {
77
auth: {
88

9-
clientId: "Enter_the_Application_Id_Here", // This is the ONLY mandatory field that you need to supply
10-
// WORKFORCE TENANT
11-
authority: "https://login.microsoftonline.com/Enter_the_Tenant_Info_Here", // Replace the placeholder with your tenant info
12-
// EXTERNAL TENANT
13-
// authority: "https://Enter_the_Tenant_Subdomain_Here.ciamlogin.com/", // Replace the placeholder with your tenant subdomain
9+
// WORKFORCE TENANT
10+
authority: "https://login.microsoftonline.com/Enter_the_Tenant_Info_Here", // Replace the placeholder with your tenant info
11+
// EXTERNAL TENANT
12+
// authority: "https://Enter_the_Tenant_Subdomain_Here.ciamlogin.com/", // Replace the placeholder with your tenant subdomain
1413
redirectUri: '/', // You must register this URI on App Registration. Defaults to window.location.href e.g. http://localhost:3000/
1514
navigateToLoginRequestUrl: true, // If "true", will navigate back to the original request location before processing the auth code response.
1615
},
@@ -48,9 +47,10 @@ const msalConfig = {
4847
* By default, MSAL.js will add OIDC scopes (openid, profile, email) to any login request.
4948
* For more information about OIDC scopes, visit:
5049
* https://learn.microsoft.com/en-us/entra/identity-platform/permissions-consent-overview#openid-connect-scopes
50+
* https://learn.microsoft.com/en-us/entra/identity-platform/permissions-consent-overview#openid-connect-scopes
5151
*/
5252
const loginRequest = {
53-
scopes: [],
53+
scopes: ["User.Read"],
5454
};
5555

5656
/**
@@ -69,4 +69,9 @@ if (typeof exports !== 'undefined') {
6969
msalConfig: msalConfig,
7070
loginRequest: loginRequest,
7171
};
72+
module.exports = {
73+
msalConfig: msalConfig,
74+
loginRequest: loginRequest,
75+
};
7276
}
77+

vanillajs-spa/App/public/authPopup.js

Lines changed: 0 additions & 91 deletions
This file was deleted.

vanillajs-spa/App/public/authRedirect.js

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)