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

Commit

Permalink
Merge pull request #58 from Azure-Samples/derisen-refactoring
Browse files Browse the repository at this point in the history
conditional logic fix
  • Loading branch information
derisen authored Apr 14, 2020
2 parents ef99a86 + 9623291 commit 5977790
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
17 changes: 10 additions & 7 deletions JavaScriptSPA/authConfig.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

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

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

// Add here scopes for access token to be used at the API endpoints.
const tokenRequest = {
scopes: apiConfig.b2cScopes, // e.g. ["https://fabrikamb2c.onmicrosoft.com/helloapi/demo.read"]
};
};
10 changes: 5 additions & 5 deletions JavaScriptSPA/authPopup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const myMSALObj = new Msal.UserAgentApplication(msalConfig);
function signIn() {
myMSALObj.loginPopup(loginRequest)
.then(loginResponse => {
console.log('id_token acquired at: ' + new Date().toString());
console.log("id_token acquired at: " + new Date().toString());
console.log(loginResponse);

if (myMSALObj.getAccount()) {
Expand All @@ -26,12 +26,12 @@ function logout() {
function getTokenPopup(request) {
return myMSALObj.acquireTokenSilent(request)
.catch(error => {
console.log("silent token acquisition fails. acquiring token using popup");
console.log("Silent token acquisition fails. Acquiring token using popup");
console.log(error);
// fallback to interaction when silent call fails
return myMSALObj.acquireTokenPopup(request)
.then(tokenResponse => {
console.log('access_token acquired at: ' + new Date().toString());
console.log("access_token acquired at: " + new Date().toString());
return tokenResponse;
}).catch(error => {
console.log(error);
Expand All @@ -43,9 +43,9 @@ function getTokenPopup(request) {
function passTokenToApi() {
getTokenPopup(tokenRequest)
.then(tokenResponse => {
console.log('access_token acquired at: ' + new Date().toString());
console.log("access_token acquired at: " + new Date().toString());
try {
logMessage("Request made to Web API:")
logMessage("Request made to Web API:");
callApiWithAccessToken(apiConfig.webApi, tokenResponse.accessToken);
} catch(err) {
console.log(err);
Expand Down
52 changes: 26 additions & 26 deletions JavaScriptSPA/authRedirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ function authRedirectCallBack(error, response) {
console.log(error);
} else {
if (response.tokenType === "id_token") {
console.log('id_token acquired at: ' + new Date().toString());
console.log("id_token acquired at: " + new Date().toString());
myMSALObj.getAccount();
getTokenRedirect(tokenRequest);
} else if (response.tokenType === "access_token") {
console.log('access_token acquired at: ' + new Date().toString());
console.log("access_token acquired at: " + new Date().toString());
accessToken = response.accessToken;
logMessage("Request made to Web API:")
if (accessToken === null || accessToken === undefined) {
logMessage("Request made to Web API:");
if (accessToken) {
try {
callApiWithAccessToken(apiConfig.webApi, accessToken)
callApiWithAccessToken(apiConfig.webApi, accessToken);
} catch (err) {
console.log(err);
}
}
} else {
console.log("token type is: " + response.tokenType);
console.log("Token type is: " + response.tokenType);
}
}
}
Expand All @@ -50,37 +50,37 @@ function logout() {

// This function can be removed if you do not need to support IE
function getTokenRedirect(request) {
return myMSALObj.acquireTokenSilent(request)
.then((response) => {
if (response.accessToken) {
accessToken = response.accessToken
logMessage("Request made to Web API:")
return myMSALObj.acquireTokenSilent(request)
.then((response) => {
if (response.accessToken) {
accessToken = response.accessToken;
logMessage("Request made to Web API:");

if (accessToken === null || accessToken === undefined) {
try {
callApiWithAccessToken(apiConfig.webApi, accessToken)
} catch (err) {
console.log(err);
if (accessToken) {
try {
callApiWithAccessToken(apiConfig.webApi, accessToken);
} catch (err) {
console.log(err);
}
}
}
}
}).catch(error => {
console.log("silent token acquisition fails. acquiring token using redirect");
console.log(error);
// fallback to interaction when silent call fails
return myMSALObj.acquireTokenRedirect(request)
});
}).catch(error => {
console.log("Silent token acquisition fails. Acquiring token using redirect");
console.log(error);
// fallback to interaction when silent call fails
return myMSALObj.acquireTokenRedirect(request);
});
}


// calls the resource API with the token
function passTokenToApi() {
if (accessToken === null || accessToken === undefined) {
if (!accessToken) {
getTokenRedirect(tokenRequest);
} else {
logMessage("Request made to Web API:")
logMessage("Request made to Web API:");
try {
callApiWithAccessToken(apiConfig.webApi, accessToken)
callApiWithAccessToken(apiConfig.webApi, accessToken);
} catch (err) {
console.log(err);
}
Expand Down

0 comments on commit 5977790

Please sign in to comment.