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

Commit 413ceaf

Browse files
committed
Added comments
1 parent 9e1645d commit 413ceaf

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

JavaScriptSPA/index.html

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,10 @@
1717
</div>
1818
</div>
1919
<script>
20-
21-
// Browser check variables
22-
var ua = window.navigator.userAgent;
23-
var msie = ua.indexOf('MSIE ');
24-
var msie11 = ua.indexOf('Trident/');
25-
var msedge = ua.indexOf('Edge/');
26-
var isIE = msie > 0 || msie11 > 0;
27-
var isEdge = msedge > 0;
28-
//If you support IE, our recommendation is that you sign-in using Redirect APIs
29-
//If you as a developer are testing using Edge InPrivate mode, please add "isEdge" to the if check
30-
31-
// can change this to default an experience outside browser use
32-
var loginType = isIE ? "REDIRECT" : "POPUP";
33-
3420
var msalConfig = {
3521
auth: {
3622
clientId: 'Enter_the_Application_Id_here', //This is your client ID
37-
authority: "https://login.microsoftonline.com/Enter_the_Tenant_Info_Here", //Default
23+
authority: "https://login.microsoftonline.com/Enter_the_Tenant_Info_Here" //This is your tenant info
3824
},
3925
cache: {
4026
cacheLocation: "localStorage",
@@ -46,23 +32,25 @@
4632
graphMeEndpoint: "https://graph.microsoft.com/v1.0/me"
4733
};
4834

49-
// this can be used for login or token request, however in more complex situations
50-
// this can have diverging options
51-
var request = {
35+
// create a request object for login or token request calls
36+
// In scenarios with incremental consent, the request object can be further customized
37+
var requestObj = {
5238
scopes: ["user.read"]
5339
};
5440

5541
var myMSALObj = new Msal.UserAgentApplication(msalConfig);
42+
5643
// Register Callbacks for redirect flow
5744
myMSALObj.handleRedirectCallbacks(acquireTokenRedirectCallBack, acquireTokenErrorRedirectCallBack);
5845

5946
function signIn() {
60-
61-
myMSALObj.loginPopup(request).then(function (loginResponse) {
62-
//Login Success
47+
myMSALObj.loginPopup(requestObj).then(function (loginResponse) {
48+
//Successful login
6349
showWelcomeMessage();
50+
//Call MS Graph using the token in the response
6451
acquireTokenPopupAndCallMSGraph();
6552
}).catch(function (error) {
53+
//Please check the console for errors
6654
console.log(error);
6755
});
6856
}
@@ -72,15 +60,15 @@
7260
}
7361

7462
function acquireTokenPopupAndCallMSGraph() {
75-
//Call acquireTokenSilent (iframe) to obtain a token for Microsoft Graph
76-
77-
myMSALObj.acquireTokenSilent(request).then(function (tokenResponse) {
63+
//Always start with acquireTokenSilent to obtain a token in the signed in user from cache
64+
myMSALObj.acquireTokenSilent(requestObj).then(function (tokenResponse) {
7865
callMSGraph(graphConfig.graphMeEndpoint, tokenResponse.accessToken, graphAPICallback);
7966
}).catch(function (error) {
80-
console.log(error.errorCode);
81-
// Call acquireTokenPopup (popup window) in case of acquireTokenSilent failure due to consent or interaction required ONLY
67+
console.log(error);
68+
// Upon acquireTokenSilent failure (due to consent or interaction or login required ONLY)
69+
// Call acquireTokenPopup(popup window)
8270
if (requiresInteraction(error.errorCode)) {
83-
myMSALObj.acquireTokenPopup(request).then(function (tokenResponse) {
71+
myMSALObj.acquireTokenPopup(requestObj).then(function (tokenResponse) {
8472
callMSGraph(graphConfig.graphMeEndpoint, tokenResponse.accessToken, graphAPICallback);
8573
}).catch(function (error) {
8674
console.log(error);
@@ -100,7 +88,6 @@
10088
xmlHttp.send();
10189
}
10290

103-
10491
function graphAPICallback(data) {
10592
document.getElementById("json").innerHTML = JSON.stringify(data, null, 2);
10693
}
@@ -113,16 +100,17 @@
113100
loginbutton.setAttribute('onclick', 'signOut();');
114101
}
115102

116-
// This function can be removed if you do not need to support IE
103+
//This function can be removed if you do not need to support IE
117104
function acquireTokenRedirectAndCallMSGraph() {
118-
//Call acquireTokenSilent (iframe) to obtain a token for Microsoft Graph
119-
myMSALObj.acquireTokenSilent(request).then(function (tokenResponse) {
105+
//Always start with acquireTokenSilent to obtain a token in the signed in user from cache
106+
myMSALObj.acquireTokenSilent(requestObj).then(function (tokenResponse) {
120107
callMSGraph(graphConfig.graphMeEndpoint, tokenResponse.accessToken, graphAPICallback);
121108
}).catch(function (error) {
122109
console.log(error);
123-
//Call acquireTokenRedirect in case of acquireToken Failure
110+
// Upon acquireTokenSilent failure (due to consent or interaction or login required ONLY)
111+
// Call acquireTokenRedirect
124112
if (requiresInteraction(error.errorCode)) {
125-
myMSALObj.acquireTokenRedirect(request);
113+
myMSALObj.acquireTokenRedirect(requestObj);
126114
}
127115
});
128116
}
@@ -148,6 +136,20 @@
148136
errorCode === "login_required";
149137
}
150138

139+
// Browser check variables
140+
var ua = window.navigator.userAgent;
141+
var msie = ua.indexOf('MSIE ');
142+
var msie11 = ua.indexOf('Trident/');
143+
var msedge = ua.indexOf('Edge/');
144+
var isIE = msie > 0 || msie11 > 0;
145+
var isEdge = msedge > 0;
146+
147+
//If you support IE, our recommendation is that you sign-in using Redirect APIs
148+
//If you as a developer are testing using Edge InPrivate mode, please add "isEdge" to the if check
149+
150+
// can change this to default an experience outside browser use
151+
var loginType = isIE ? "REDIRECT" : "POPUP";
152+
151153
// runs on page load, change config to try different login types to see what is best for your application
152154
if (loginType === 'POPUP') {
153155
if (myMSALObj.getAccount()) {// avoid duplicate code execution on page load in case of iframe and popup window.
@@ -157,7 +159,7 @@
157159
}
158160
else if (loginType === 'REDIRECT') {
159161
document.getElementById("SignIn").onclick = function () {
160-
myMSALObj.loginRedirect(request);
162+
myMSALObj.loginRedirect(requestObj);
161163
};
162164

163165
if (myMSALObj.getAccount() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window.

0 commit comments

Comments
 (0)