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

Commit 031ada2

Browse files
author
Prithvi Kanherkar
committed
Refactored to separate functionality
Redirect and popup code is now separated
1 parent d680af4 commit 031ada2

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

JavaScriptSPA/index.html

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ <h4 id="WelcomeMessage"></h4>
2323
var isEdge = msedge > 0;
2424

2525
var applicationConfig = {
26-
clientID: "Enter_the_Application_Id_here", //This is your client ID
26+
clientID: "0813e1d1-ad72-46a9-8665-399bba48c201", //This is your client ID
2727
graphScopes: ["user.read"],
2828
graphEndpoint: "https://graph.microsoft.com/v1.0/me"
2929
};
3030

3131
//Pass null for default authority (https://login.microsoftonline.com/common) and tokenReceivedCallback if using popup apis
32-
var myMSALObj = new Msal.UserAgentApplication(applicationConfig.clientID, null, null, {storeAuthStateInCookie:true});
32+
var myMSALObj = new Msal.UserAgentApplication(applicationConfig.clientID, null, null, {storeAuthStateInCookie:true, cacheLocation:"localStorage"});
3333

3434
function signIn() {
3535
myMSALObj.loginPopup(applicationConfig.graphScopes).then(function (idToken) {
@@ -50,31 +50,12 @@ <h4 id="WelcomeMessage"></h4>
5050
myMSALObj.acquireTokenSilent(applicationConfig.graphScopes).then(function (accessToken) {
5151
callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback);
5252
}, function (error) {
53-
if (isIE) {
54-
// NOTE: only for IE browsers
55-
// Call acquireTokenRedirect in case of acquireToken Failure
56-
if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1) {
57-
myMSALObj.acquireTokenRedirect(applicationConfig.graphScopes);
58-
}
59-
}
60-
// Uncomment this if you are testing in Edge InPrivate, or delete
61-
/**
62-
else if(msedge > 0) {
63-
// NOTE: only for Edge InPrivate browsers
64-
// Call acquireTokenRedirect in case of acquireToken Failure
65-
if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1) {
66-
myMSALObj.acquireTokenRedirect(applicationConfig.graphScopes);
67-
}
68-
}
69-
**/
70-
else {
71-
// Call acquireTokenPopup (popup window) in case of acquireToken Failure
72-
if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1) {
73-
myMSALObj.acquireTokenPopup(applicationConfig.graphScopes).then(function (accessToken) {
74-
callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback);
75-
}, function (error) {
76-
});
77-
}
53+
// Call acquireTokenPopup (popup window) in case of acquireToken Failure
54+
if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1) {
55+
myMSALObj.acquireTokenPopup(applicationConfig.graphScopes).then(function (accessToken) {
56+
callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback);
57+
}, function (error) {
58+
});
7859
}
7960
});
8061
}
@@ -104,24 +85,54 @@ <h4 id="WelcomeMessage"></h4>
10485
loginbutton.setAttribute('onclick', 'signOut();');
10586
}
10687

107-
// Remove this code if IE is not used
88+
/**
89+
if (myMSALObj.getUser() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window.
90+
showWelcomeMessage();
91+
acquireTokenAndCallMSGraph();
92+
}
93+
**/
94+
95+
// Uncomment the above code and delete the below code if you are not using browser detection
96+
10897
if(isIE) {
10998
document.getElementById("SignIn").onclick = function() {
11099
myMSALObj.loginRedirect(applicationConfig.graphScopes);
111100
};
101+
if (myMSALObj.getUser() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window.
102+
showWelcomeMessage();
103+
acquireTokenRedirectAndCallMSGraph();
104+
}
112105
}
113106
// Uncomment this if you are testing in Edge InPrivate, or delete
114107
/**
115108
else if(isEdge) {
116109
document.getElementById("SignIn").onclick = function () {
117110
myMSALObj.loginRedirect(applicationConfig.graphScopes);
118111
};
112+
if (myMSALObj.getUser() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window.
113+
showWelcomeMessage();
114+
acquireTokenRedirectAndCallMSGraph();
115+
}
119116
}
120117
**/
121-
122-
if (myMSALObj.getUser() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window.
123-
showWelcomeMessage();
124-
acquireTokenAndCallMSGraph();
118+
else {
119+
if (myMSALObj.getUser() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window.
120+
showWelcomeMessage();
121+
acquireTokenAndCallMSGraph();
122+
}
123+
}
124+
125+
// This function can be removed if browser detection isn't needed
126+
function acquireTokenRedirectAndCallMSGraph(myMSALObj) {
127+
//Call acquireTokenSilent (iframe) to obtain a token for Microsoft Graph
128+
myMSALObj.acquireTokenSilent(applicationConfig.graphScopes).then(function (accessToken) {
129+
callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback);
130+
}, function (error) {
131+
//Call acquireTokenRedirect in case of acquireToken Failure
132+
if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1) {
133+
myMSALObj.acquireTokenRedirect(applicationConfig.graphScopes);
134+
}
135+
});
125136
}
126137

127138
</script>

server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var morgan = require('morgan');
99
var path = require('path');
1010

1111
// Initialize variables.
12-
var port = 30662; // process.env.PORT || 30662;
12+
var port = 1530; // process.env.PORT || 30662;
1313

1414
// Configure morgan module to log all requests.
1515
app.use(morgan('dev'));

0 commit comments

Comments
 (0)