Skip to content
This repository has been archived by the owner. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 29 additions & 34 deletions JavaScriptSPA/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>Quickstart for MSAL JS</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.4/bluebird.min.js"></script>
<script src="/msal-1.0.0-preview.4.min.js"></script>
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.0/js/msal.js"></script>
</head>

<body>
Expand All @@ -17,14 +17,18 @@
</div>
</div>
<script>
// can change this to default an experience outside browser use
var loginType = isIE ? "REDIRECT" : "POPUP";
var isCookie = isIE ? true : false;

var msalConfig = {
auth: {
clientId: 'Enter_the_Application_Id_here', //This is your client ID
clientId: "Enter_the_Application_Id_here", //This is your client ID
authority: "https://login.microsoftonline.com/Enter_the_Tenant_Info_Here" //This is your tenant info
},
cache: {
cacheLocation: "localStorage",
storeAuthStateInCookie: true
storeAuthStateInCookie: isCookie
}
};

Expand All @@ -34,17 +38,19 @@

// create a request object for login or token request calls
// In scenarios with incremental consent, the request object can be further customized
var requestObj = {
var request = {
scopes: ["user.read"]
};

var myMSALObj = new Msal.UserAgentApplication(msalConfig);

// Register Callbacks for redirect flow
myMSALObj.handleRedirectCallbacks(acquireTokenRedirectCallBack, acquireTokenErrorRedirectCallBack);
if (loginType === "REDIRECT") {
myMSALObj.handleRedirectCallback(authRedirectCallBack);
}

function signIn() {
myMSALObj.loginPopup(requestObj).then(function (loginResponse) {
myMSALObj.loginPopup(request).then(function (loginResponse) {
//Successful login
showWelcomeMessage();
//Call MS Graph using the token in the response
Expand All @@ -61,14 +67,15 @@

function acquireTokenPopupAndCallMSGraph() {
//Always start with acquireTokenSilent to obtain a token in the signed in user from cache
myMSALObj.acquireTokenSilent(requestObj).then(function (tokenResponse) {
myMSALObj.acquireTokenSilent(request).then(function (tokenResponse) {
callMSGraph(graphConfig.graphMeEndpoint, tokenResponse.accessToken, graphAPICallback);
}).catch(function (error) {
}).catch(error => {
// dump in console for easy debug
console.log(error);
// Upon acquireTokenSilent failure (due to consent or interaction or login required ONLY)
// Call acquireTokenPopup(popup window)
if (requiresInteraction(error.errorCode)) {
myMSALObj.acquireTokenPopup(requestObj).then(function (tokenResponse) {
// could also check if err instance of InteractionRequiredAuthError if you can import the class.
if (error.name === "InteractionRequiredAuthError") {
myMSALObj.acquireTokenPopup(request).then(function (tokenResponse) {
callMSGraph(graphConfig.graphMeEndpoint, tokenResponse.accessToken, graphAPICallback);
}).catch(function (error) {
console.log(error);
Expand Down Expand Up @@ -103,37 +110,28 @@
//This function can be removed if you do not need to support IE
function acquireTokenRedirectAndCallMSGraph() {
//Always start with acquireTokenSilent to obtain a token in the signed in user from cache
myMSALObj.acquireTokenSilent(requestObj).then(function (tokenResponse) {
myMSALObj.acquireTokenSilent(request).then(function (tokenResponse) {
callMSGraph(graphConfig.graphMeEndpoint, tokenResponse.accessToken, graphAPICallback);
}).catch(function (error) {
console.log(error);
// Upon acquireTokenSilent failure (due to consent or interaction or login required ONLY)
// Call acquireTokenRedirect
if (requiresInteraction(error.errorCode)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sameerag There is a reference here to the requiresInteraction method in the redirect flow which is removed. This will cause redirect to fail I think.

myMSALObj.acquireTokenRedirect(requestObj);
myMSALObj.acquireTokenRedirect(request);
}
});
}

function acquireTokenRedirectCallBack(response) {
if (response.tokenType === "access_token") {
callMSGraph(graphConfig.graphMeEndpoint, response.accessToken, graphAPICallback);
function authRedirectCallBack(error, response) {
if (error) {
console.log(error);
} else {
console.log("token type is:" + response.tokenType);
}
}

function acquireTokenErrorRedirectCallBack(error) {
console.log(error);
}

function requiresInteraction(errorCode) {
if (!errorCode || !errorCode.length) {
return false;
if (response.tokenType === "access_token") {
callMSGraph(graphConfig.graphMeEndpoint, response.accessToken, graphAPICallback);
} else {
console.log("token type is:" + response.tokenType);
}
}
return errorCode === "consent_required" ||
errorCode === "interaction_required" ||
errorCode === "login_required";
}

// Browser check variables
Expand All @@ -147,9 +145,6 @@
//If you support IE, our recommendation is that you sign-in using Redirect APIs
//If you as a developer are testing using Edge InPrivate mode, please add "isEdge" to the if check

// can change this to default an experience outside browser use
var loginType = isIE ? "REDIRECT" : "POPUP";

// runs on page load, change config to try different login types to see what is best for your application
if (loginType === 'POPUP') {
if (myMSALObj.getAccount()) {// avoid duplicate code execution on page load in case of iframe and popup window.
Expand All @@ -159,7 +154,7 @@
}
else if (loginType === 'REDIRECT') {
document.getElementById("SignIn").onclick = function () {
myMSALObj.loginRedirect(requestObj);
myMSALObj.loginRedirect(request);
};

if (myMSALObj.getAccount() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window.
Expand Down
17 changes: 0 additions & 17 deletions JavaScriptSPA/msal-1.0.0-preview.4.min.js

This file was deleted.