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

Commit 24e5cf4

Browse files
committed
decoupling login from access token request
1 parent 83caf6d commit 24e5cf4

File tree

6 files changed

+13
-12
lines changed

6 files changed

+13
-12
lines changed

JavaScriptSPA/api.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ function callApiWithAccessToken(endpoint, accessToken) {
1010
headers: headers
1111
};
1212

13-
console.log('request made to Web API at: ' + new Date().toString());
1413

1514
fetch(endpoint, options)
1615
.then(response => response.json())
1716
.then(response => {
18-
console.log('web API responded at: ' + new Date().toString());
19-
logMessage("web API returned:\n" + JSON.stringify(response));
17+
logMessage("Web API returned:\n" + JSON.stringify(response));
2018
}).catch(error => {
2119
logMessage("Error calling the Web api:\n" + error);
2220
});
@@ -26,7 +24,9 @@ function callApiWithAccessToken(endpoint, accessToken) {
2624
function callApi() {
2725
getTokenPopup(tokenRequest)
2826
.then(tokenResponse => {
27+
console.log(tokenResponse)
2928
try {
29+
logMessage("\nRequest made to Web API:\n")
3030
callApiWithAccessToken(apiConfig.webApi, tokenResponse.accessToken);
3131
} catch(err) {
3232
console.log(err);

JavaScriptSPA/apiConfig.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ const apiConfig = {
44
webApi: "https://fabrikamb2chello.azurewebsites.net/hello"
55
};
66

7-
// request to sign-in (returns an idToken)
8-
const loginRequest = {
9-
scopes: apiConfig.b2cScopes
10-
};
11-
127
// request to acquire a token for resource access
138
const tokenRequest = {
149
scopes: apiConfig.b2cScopes

JavaScriptSPA/auth.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function authRedirectCallBack(error, response) {
2727
if (response.tokenType === "id_token" && myMSALObj.getAccount() && !myMSALObj.isCallback(window.location.hash)) {
2828
console.log('id_token acquired at: ' + new Date().toString());
2929
updateUI();
30-
getTokenRedirect(loginRequest);
30+
getTokenRedirect(tokenRequest);
3131
} else if (response.tokenType === "access_token") {
3232
console.log('access_token acquired at: ' + new Date().toString());
3333
} else {
@@ -36,7 +36,7 @@ function authRedirectCallBack(error, response) {
3636
}
3737
}
3838

39-
// Redirect: once login is successful and redirects with tokens, call Graph API
39+
// Redirect: once login is successful and redirects with tokens, update UI
4040
if (myMSALObj.getAccount() && !myMSALObj.isCallback(window.location.hash)) {
4141
// avoid duplicate code execution on page load in case of iframe and Popup window.
4242
updateUI();
@@ -50,6 +50,7 @@ function signIn(method) {
5050
if (signInType === "Popup") {
5151
myMSALObj.loginPopup(loginRequest)
5252
.then(loginResponse => {
53+
console.log(loginResponse);
5354
console.log('id_token acquired at: ' + new Date().toString());
5455
if (myMSALObj.getAccount()) {
5556
updateUI();

JavaScriptSPA/authConfig.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ const msalConfig = {
88
cacheLocation: "localStorage",
99
storeAuthStateInCookie: true
1010
}
11+
};
12+
13+
// request to sign-in (returns an idToken)
14+
const loginRequest = {
15+
scopes: ["openid", "profile"],
1116
};

JavaScriptSPA/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
<body>
2323
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
24-
<a class="navbar-brand" href="/">MS Identity Platform</a>
24+
<a class="navbar-brand" href="/">Azure AD B2C</a>
2525
<div class="btn-group ml-auto dropleft">
2626
<button type="button" id="SignIn" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
2727
Sign In

JavaScriptSPA/ui.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ function updateUI() {
2323
// debug helper
2424
function logMessage(s) {
2525
document.getElementById("response")
26-
.appendChild(document.createTextNode('\n\n' + s));
26+
.appendChild(document.createTextNode('\n' + s));
2727
}

0 commit comments

Comments
 (0)