|
1 | | -<!DOCTYPE html> |
| 1 | +<!DOCTYPE html> |
2 | 2 | <html> |
3 | 3 | <head> |
4 | | - <!-- bootstrap reference used for styling the page --> |
5 | | - <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> |
6 | | - <title>JavaScript SPA Guided Setup</title> |
| 4 | + <title>Quickstart for MSAL JS</title> |
| 5 | + <script src="https://secure.aadcdn.microsoftonline-p.com/lib/0.2.3/js/msal.js"></script> |
| 6 | + <script src="https://code.jquery.com/jquery-3.2.1.min.js" class="pre"></script> |
| 7 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.4/bluebird.min.js" class="pre"></script> |
| 8 | + <style> |
| 9 | + table, td, th { |
| 10 | + border: 1px solid black; |
| 11 | + } |
| 12 | + </style> |
7 | 13 | </head> |
8 | | -<body style="margin: 40px"> |
9 | | - <button id="callGraphButton" type="button" class="btn btn-primary" onclick="callGraphApi()">Call Microsoft Graph API</button> |
10 | | - <div id="errorMessage" class="text-danger"></div> |
11 | | - <div class="hidden"> |
12 | | - <h3>Graph API Call Response</h3> |
13 | | - <pre class="well" id="graphResponse"></pre> |
14 | | - </div> |
15 | | - <div class="hidden"> |
16 | | - <h3>Access Token</h3> |
17 | | - <pre class="well" id="accessToken"></pre> |
18 | | - </div> |
19 | | - <div class="hidden"> |
20 | | - <h3>ID Token Claims</h3> |
21 | | - <pre class="well" id="userInfo"></pre> |
22 | | - </div> |
23 | | - <button id="signOutButton" type="button" class="btn btn-primary hidden" onclick="signOut()">Sign out</button> |
24 | | - |
25 | | - <!-- This app uses cdn to reference msal.js (recommended). |
26 | | - You can also download it from: https://github.com/AzureAD/microsoft-authentication-library-for-js --> |
27 | | - <script src="https://secure.aadcdn.microsoftonline-p.com/lib/0.2.3/js/msal.min.js"></script> |
28 | | - <!--<script type="text/javascript" src="msal.js"></script>--> |
29 | | - <script type="text/javascript" src="msalconfig.js"></script> |
30 | | - |
31 | | - <!-- The 'bluebird' and 'fetch' references below are required if you need to run this application on Internet Explorer --> |
32 | | - <script src="//cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.4/bluebird.min.js"></script> |
33 | | - <script src="//cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.min.js"></script> |
34 | | - |
35 | | - <script type="text/javascript" src="app.js"></script> |
| 14 | + |
| 15 | +<body> |
| 16 | + <h2>Welcome to MSAL.js Quickstart</h2><br/> |
| 17 | + <h4 id="WelcomeMessage"></h4> |
| 18 | + <button id="SignIn" onclick="signIn()">Sign In</button><br/><br/> |
| 19 | + <table id="graphData"></table> |
| 20 | + |
| 21 | + <script> |
| 22 | + var applicationConfig = { |
| 23 | + clientID: "Enter_the_Application_Id_here", //This is your client ID |
| 24 | + graphScopes: ["user.read"], |
| 25 | + graphEndpoint: "https://graph.microsoft.com/v1.0/me" |
| 26 | + }; |
| 27 | + |
| 28 | + //Pass null for default authority (https://login.microsoftonline.com/common) and tokenReceivedCallback if using popup apis |
| 29 | + var myMSALObj = new Msal.UserAgentApplication(applicationConfig.clientID, null, null); |
| 30 | + |
| 31 | + function signIn() { |
| 32 | + myMSALObj.loginPopup(applicationConfig.graphScopes).then(function (idToken) { |
| 33 | + //Login Success |
| 34 | + showWelcomeMessage(); |
| 35 | + acquireTokenAndCallMSGraph(); |
| 36 | + }, function (error) { |
| 37 | + console.log(error); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + function signOut() { |
| 42 | + myMSALObj.logout(); |
| 43 | + } |
| 44 | + |
| 45 | + function acquireTokenAndCallMSGraph() { |
| 46 | + //Call acquireTokenSilent (hidden iframe) to obtain a token for Microsoft Graph |
| 47 | + myMSALObj.acquireTokenSilent(applicationConfig.graphScopes).then(function (accessToken) { |
| 48 | + callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback); |
| 49 | + }, function (error) { |
| 50 | + //Call acquireTokenPopup (popup window) in case of acquireToken Failure |
| 51 | + if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1) { |
| 52 | + myMSALObj.acquireTokenPopup(applicationConfig.graphScopes).then(function (accessToken) { |
| 53 | + callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback); |
| 54 | + }, function (error) { |
| 55 | + }); |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + function callMSGraph(theUrl, accessToken, callback) { |
| 61 | + var xmlHttp = new XMLHttpRequest(); |
| 62 | + xmlHttp.onreadystatechange = function () { |
| 63 | + if (this.readyState == 4 && this.status == 200) |
| 64 | + callback(JSON.parse(this.responseText)); |
| 65 | + } |
| 66 | + xmlHttp.open("GET", theUrl, true); // true for asynchronous |
| 67 | + xmlHttp.setRequestHeader('Authorization', 'Bearer ' + accessToken); |
| 68 | + xmlHttp.send(); |
| 69 | + } |
| 70 | + |
| 71 | + function graphAPICallback(data) { |
| 72 | + //Display user data on DOM |
| 73 | + var divWelcome = document.getElementById('WelcomeMessage'); |
| 74 | + divWelcome.innerHTML += " to Microsoft Graph API!!"; |
| 75 | + $.each(data, function (key, value) { |
| 76 | + var tr = $('<tr></tr>'); |
| 77 | + $('<td>' + '<i>' + key + '</i>' + '</td>').appendTo(tr); |
| 78 | + $('<td>' + value + '</td>').appendTo(tr); |
| 79 | + tr.appendTo('#graphData'); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + function showWelcomeMessage() { |
| 84 | + var divWelcome = document.getElementById('WelcomeMessage'); |
| 85 | + divWelcome.innerHTML += 'Welcome ' + myMSALObj.getUser().name; |
| 86 | + var loginbutton = document.getElementById('SignIn'); |
| 87 | + loginbutton.innerHTML = 'Sign Out'; |
| 88 | + loginbutton.setAttribute('onclick', 'signOut();'); |
| 89 | + } |
| 90 | + |
| 91 | + if (myMSALObj.getUser() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window. |
| 92 | + showWelcomeMessage(); |
| 93 | + acquireTokenAndCallMSGraph(); |
| 94 | + } |
| 95 | + </script> |
36 | 96 | </body> |
37 | 97 | </html> |
0 commit comments