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

Commit 83caf6d

Browse files
committed
added redirect flow
1 parent 394b2c7 commit 83caf6d

File tree

4 files changed

+94
-31
lines changed

4 files changed

+94
-31
lines changed

JavaScriptSPA/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function callApiWithAccessToken(endpoint, accessToken) {
2424

2525
// calls the resource API with the token
2626
function callApi() {
27-
getToken(tokenRequest)
27+
getTokenPopup(tokenRequest)
2828
.then(tokenResponse => {
2929
try {
3030
callApiWithAccessToken(apiConfig.webApi, tokenResponse.accessToken);

JavaScriptSPA/auth.js

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,94 @@
11

22
"use strict";
33

4-
// instantiate MSAL
4+
// Browser check variables
5+
// If you support IE, our recommendation is that you sign-in using Redirect APIs
6+
// If you as a developer are testing using Edge InPrivate mode, please add "isEdge" to the if check
7+
const ua = window.navigator.userAgent;
8+
const msie = ua.indexOf("MSIE ");
9+
const msie11 = ua.indexOf("Trident/");
10+
const msedge = ua.indexOf("Edge/");
11+
const isIE = msie > 0 || msie11 > 0;
12+
const isEdge = msedge > 0;
13+
14+
let signInType;
15+
16+
// Create the main myMSALObj instance
17+
// configuration parameters are located at authConfig.js
518
const myMSALObj = new Msal.UserAgentApplication(msalConfig);
619

7-
// sign-in and acquire a token silently with popup flow.
8-
// Fall back in case of failure with silent acquisition to popup.
9-
function signIn() {
20+
// Register Callbacks for Redirect flow
21+
myMSALObj.handleRedirectCallback(authRedirectCallBack);
22+
23+
function authRedirectCallBack(error, response) {
24+
if (error) {
25+
console.log(error);
26+
} else {
27+
if (response.tokenType === "id_token" && myMSALObj.getAccount() && !myMSALObj.isCallback(window.location.hash)) {
28+
console.log('id_token acquired at: ' + new Date().toString());
29+
updateUI();
30+
getTokenRedirect(loginRequest);
31+
} else if (response.tokenType === "access_token") {
32+
console.log('access_token acquired at: ' + new Date().toString());
33+
} else {
34+
console.log("token type is:" + response.tokenType);
35+
}
36+
}
37+
}
38+
39+
// Redirect: once login is successful and redirects with tokens, call Graph API
40+
if (myMSALObj.getAccount() && !myMSALObj.isCallback(window.location.hash)) {
41+
// avoid duplicate code execution on page load in case of iframe and Popup window.
42+
updateUI();
43+
}
44+
45+
46+
function signIn(method) {
47+
48+
signInType = isIE ? "Redirect" : method;
49+
50+
if (signInType === "Popup") {
1051
myMSALObj.loginPopup(loginRequest)
11-
.then(loginResponse => {
12-
getToken(tokenRequest)
13-
.then(updateUI);
14-
}).catch(error => {
15-
logMessage(error);
52+
.then(loginResponse => {
53+
console.log('id_token acquired at: ' + new Date().toString());
54+
if (myMSALObj.getAccount()) {
55+
updateUI();
56+
}
57+
}).catch(function (error) {
58+
console.log(error);
1659
});
60+
61+
} else if (signInType === "Redirect") {
62+
myMSALObj.loginRedirect(loginRequest)
63+
}
64+
}
65+
66+
// sign-out the user
67+
function logout() {
68+
// Removes all sessions, need to call AAD endpoint to do full logout
69+
myMSALObj.logout();
1770
}
1871

19-
//acquire a token silently
20-
function getToken(tokenRequest) {
21-
return myMSALObj.acquireTokenSilent(tokenRequest)
72+
function getTokenPopup(request) {
73+
return myMSALObj.acquireTokenSilent(request)
2274
.catch(error => {
23-
console.log("Silent token acquisition fails. acquiring token using popup");
24-
75+
console.log("silent token acquisition fails. acquiring token using popup");
2576
// fallback to interaction when silent call fails
26-
return myMSALObj.acquireTokenPopup(tokenRequest)
77+
return myMSALObj.acquireTokenPopup(request)
2778
.then(tokenResponse => {
2879
}).catch(error => {
29-
logMessage("Failed token acquisition", error);
80+
console.log(error);
3081
});
3182
});
32-
}
33-
34-
// sign-out the user
35-
function logout() {
36-
// Removes all sessions, need to call AAD endpoint to do full logout
37-
myMSALObj.logout();
38-
}
83+
}
84+
85+
// This function can be removed if you do not need to support IE
86+
function getTokenRedirect(request) {
87+
return myMSALObj.acquireTokenSilent(request)
88+
.then((response) => {
89+
}).catch(error => {
90+
console.log("silent token acquisition fails. acquiring token using redirect");
91+
// fallback to interaction when silent call fails
92+
return myMSALObj.acquireTokenRedirect(request)
93+
});
94+
}

JavaScriptSPA/index.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@
2121

2222
<body>
2323
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
24-
<a class="navbar-brand" href="/">Azure AD B2C</a>
25-
<button type="button" id="authButton" class="btn btn-secondary ml-auto" onclick="signIn()">
24+
<a class="navbar-brand" href="/">MS Identity Platform</a>
25+
<div class="btn-group ml-auto dropleft">
26+
<button type="button" id="SignIn" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
2627
Sign In
27-
</button>
28+
</button>
29+
<div class="dropdown-menu">
30+
<button class="dropdown-item" id="Popup" onclick="signIn(this.id)">Sign in using Popup</button>
31+
<button class="dropdown-item" id="Redirect" onclick="signIn(this.id)">Sign in using Redirect</button>
32+
</div>
33+
</div>
2834
</nav>
2935
<div class="card">
3036
<h5 class="card-header text-center">Getting an access token with Azure AD B2C and calling a Web API</h5>

JavaScriptSPA/ui.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ function updateUI() {
44
logMessage("User '" + userName + "' logged-in");
55

66
// add the logout button
7-
const authButton = document.getElementById('authButton');
8-
authButton.innerHTML = 'logout';
9-
authButton.setAttribute('onclick', 'logout();');
10-
authButton.setAttribute('class', "btn btn-success ml-auto")
7+
const signInButton = document.getElementById('SignIn');
8+
signInButton.nextElementSibling.style.display = 'none';
9+
signInButton.innerHTML = 'logout';
10+
signInButton.setAttribute('onclick', 'logout();');
11+
signInButton.setAttribute('class', "btn btn-success ml-auto")
1112

1213
// greet the user - specifying login
1314
const label = document.getElementById('label');

0 commit comments

Comments
 (0)