Skip to content
This repository has been archived by the owner. It is now read-only.

Commit 5977790

Browse files
authored
Merge pull request #58 from Azure-Samples/derisen-refactoring
conditional logic fix
2 parents ef99a86 + 9623291 commit 5977790

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

JavaScriptSPA/authConfig.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11

2-
// Config object to be passed to Msal on creation.
3-
// For a full list of msal.js configuration parameters,
4-
// visit https://azuread.github.io/microsoft-authentication-library-for-js/docs/msal/modules/_configuration_.html
2+
/**
3+
* Config object to be passed to MSAL on creation.
4+
* For a full list of msal.js configuration parameters,
5+
* visit https://azuread.github.io/microsoft-authentication-library-for-js/docs/msal/modules/_configuration_.html
6+
* */
57
const msalConfig = {
68
auth: {
79
clientId: "e760cab2-b9a1-4c0d-86fb-ff7084abd902",
@@ -14,14 +16,15 @@ const msalConfig = {
1416
}
1517
};
1618

17-
// Add here scopes for id token to be used at the MS Identity Platform endpoint
18-
// For a full list of available authentication parameters,
19-
// visit https://azuread.github.io/microsoft-authentication-library-for-js/docs/msal/modules/_authenticationparameters_.html
19+
/**
20+
* Scopes you enter here will be consented once you authenticate. For a full list of available authentication parameters,
21+
* visit https://azuread.github.io/microsoft-authentication-library-for-js/docs/msal/modules/_authenticationparameters_.html
22+
*/
2023
const loginRequest = {
2124
scopes: ["openid", "profile"],
2225
};
2326

2427
// Add here scopes for access token to be used at the API endpoints.
2528
const tokenRequest = {
2629
scopes: apiConfig.b2cScopes, // e.g. ["https://fabrikamb2c.onmicrosoft.com/helloapi/demo.read"]
27-
};
30+
};

JavaScriptSPA/authPopup.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const myMSALObj = new Msal.UserAgentApplication(msalConfig);
55
function signIn() {
66
myMSALObj.loginPopup(loginRequest)
77
.then(loginResponse => {
8-
console.log('id_token acquired at: ' + new Date().toString());
8+
console.log("id_token acquired at: " + new Date().toString());
99
console.log(loginResponse);
1010

1111
if (myMSALObj.getAccount()) {
@@ -26,12 +26,12 @@ function logout() {
2626
function getTokenPopup(request) {
2727
return myMSALObj.acquireTokenSilent(request)
2828
.catch(error => {
29-
console.log("silent token acquisition fails. acquiring token using popup");
29+
console.log("Silent token acquisition fails. Acquiring token using popup");
3030
console.log(error);
3131
// fallback to interaction when silent call fails
3232
return myMSALObj.acquireTokenPopup(request)
3333
.then(tokenResponse => {
34-
console.log('access_token acquired at: ' + new Date().toString());
34+
console.log("access_token acquired at: " + new Date().toString());
3535
return tokenResponse;
3636
}).catch(error => {
3737
console.log(error);
@@ -43,9 +43,9 @@ function getTokenPopup(request) {
4343
function passTokenToApi() {
4444
getTokenPopup(tokenRequest)
4545
.then(tokenResponse => {
46-
console.log('access_token acquired at: ' + new Date().toString());
46+
console.log("access_token acquired at: " + new Date().toString());
4747
try {
48-
logMessage("Request made to Web API:")
48+
logMessage("Request made to Web API:");
4949
callApiWithAccessToken(apiConfig.webApi, tokenResponse.accessToken);
5050
} catch(err) {
5151
console.log(err);

JavaScriptSPA/authRedirect.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ function authRedirectCallBack(error, response) {
1212
console.log(error);
1313
} else {
1414
if (response.tokenType === "id_token") {
15-
console.log('id_token acquired at: ' + new Date().toString());
15+
console.log("id_token acquired at: " + new Date().toString());
1616
myMSALObj.getAccount();
1717
getTokenRedirect(tokenRequest);
1818
} else if (response.tokenType === "access_token") {
19-
console.log('access_token acquired at: ' + new Date().toString());
19+
console.log("access_token acquired at: " + new Date().toString());
2020
accessToken = response.accessToken;
21-
logMessage("Request made to Web API:")
22-
if (accessToken === null || accessToken === undefined) {
21+
logMessage("Request made to Web API:");
22+
if (accessToken) {
2323
try {
24-
callApiWithAccessToken(apiConfig.webApi, accessToken)
24+
callApiWithAccessToken(apiConfig.webApi, accessToken);
2525
} catch (err) {
2626
console.log(err);
2727
}
2828
}
2929
} else {
30-
console.log("token type is: " + response.tokenType);
30+
console.log("Token type is: " + response.tokenType);
3131
}
3232
}
3333
}
@@ -50,37 +50,37 @@ function logout() {
5050

5151
// This function can be removed if you do not need to support IE
5252
function getTokenRedirect(request) {
53-
return myMSALObj.acquireTokenSilent(request)
54-
.then((response) => {
55-
if (response.accessToken) {
56-
accessToken = response.accessToken
57-
logMessage("Request made to Web API:")
53+
return myMSALObj.acquireTokenSilent(request)
54+
.then((response) => {
55+
if (response.accessToken) {
56+
accessToken = response.accessToken;
57+
logMessage("Request made to Web API:");
5858

59-
if (accessToken === null || accessToken === undefined) {
60-
try {
61-
callApiWithAccessToken(apiConfig.webApi, accessToken)
62-
} catch (err) {
63-
console.log(err);
59+
if (accessToken) {
60+
try {
61+
callApiWithAccessToken(apiConfig.webApi, accessToken);
62+
} catch (err) {
63+
console.log(err);
64+
}
6465
}
6566
}
66-
}
67-
}).catch(error => {
68-
console.log("silent token acquisition fails. acquiring token using redirect");
69-
console.log(error);
70-
// fallback to interaction when silent call fails
71-
return myMSALObj.acquireTokenRedirect(request)
72-
});
67+
}).catch(error => {
68+
console.log("Silent token acquisition fails. Acquiring token using redirect");
69+
console.log(error);
70+
// fallback to interaction when silent call fails
71+
return myMSALObj.acquireTokenRedirect(request);
72+
});
7373
}
7474

7575

7676
// calls the resource API with the token
7777
function passTokenToApi() {
78-
if (accessToken === null || accessToken === undefined) {
78+
if (!accessToken) {
7979
getTokenRedirect(tokenRequest);
8080
} else {
81-
logMessage("Request made to Web API:")
81+
logMessage("Request made to Web API:");
8282
try {
83-
callApiWithAccessToken(apiConfig.webApi, accessToken)
83+
callApiWithAccessToken(apiConfig.webApi, accessToken);
8484
} catch (err) {
8585
console.log(err);
8686
}

0 commit comments

Comments
 (0)