|
3 | 3 | <head> |
4 | 4 | <title>Quickstart for MSAL JS</title> |
5 | 5 | <script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.4/bluebird.min.js"></script> |
6 | | - <script src="/msal-0.9.0.min.js"></script> |
| 6 | + <script src="/msal-1.0.0-preview.min.js"></script> |
7 | 7 | <link rel="stylesheet" type="text/css" href="/style.css"> |
8 | 8 | </head> |
9 | 9 |
|
|
18 | 18 | </div> |
19 | 19 | </div> |
20 | 20 | <script> |
21 | | - var applicationConfig = { |
22 | | - clientID: "245e9392-c666-4d51-8f8a-bfd9e55b2456", |
23 | | - authority: "https://login.microsoftonline.com/common", |
24 | | - graphScopes: ["user.read", "Mail.Send"], |
25 | | - graphEndpoint: "https://graph.microsoft.com/v1.0/me" |
26 | | - }; |
27 | 21 |
|
28 | | - var config = { |
| 22 | + // Browser check variables |
| 23 | + var ua = window.navigator.userAgent; |
| 24 | + var msie = ua.indexOf('MSIE '); |
| 25 | + var msie11 = ua.indexOf('Trident/'); |
| 26 | + var msedge = ua.indexOf('Edge/'); |
| 27 | + var isIE = msie > 0 || msie11 > 0; |
| 28 | + var isEdge = msedge > 0; |
| 29 | + //If you support IE, our recommendation is that you sign-in using Redirect APIs |
| 30 | + //If you as a developer are testing using Edge InPrivate mode, please add "isEdge" to the if check |
| 31 | + |
| 32 | + // can change this to default an experience outside browser use |
| 33 | + var loginType = isIE ? "REDIRECT" : "POPUP"; |
| 34 | + |
| 35 | + var msalConfig = { |
29 | 36 | auth: { |
30 | | - clientId: applicationConfig.clientID, |
31 | | - authority: applicationConfig.authority, |
32 | | - validateAuthority: true |
| 37 | + clientId: 'Enter_the_Application_Id_here', //This is your client ID |
| 38 | + authority: "https://login.microsoftonline.com/Enter_the_Tenant_Info_Here", //Default authority |
33 | 39 | }, |
34 | 40 | cache: { |
35 | 41 | cacheLocation: "localStorage", |
36 | 42 | storeAuthStateInCookie: true |
37 | 43 | } |
38 | 44 | }; |
39 | 45 |
|
40 | | - var myMSALObj = new Msal.UserAgentApplication(config); |
| 46 | + var graphConfig = { |
| 47 | + graphMeEndpoint: "https://graph.microsoft.com/v1.0/me" |
| 48 | + }; |
| 49 | + |
| 50 | + // this can be used for login or token request, however in more complex situations |
| 51 | + // this can have diverging options |
| 52 | + var request = { |
| 53 | + scopes: ["user.read"] |
| 54 | + }; |
| 55 | + |
| 56 | + var myMSALObj = new Msal.UserAgentApplication(msalConfig); |
| 57 | + // Register Callbacks for redirect flow |
41 | 58 | myMSALObj.handleRedirectCallbacks(acquireTokenRedirectCallBack, acquireTokenErrorRedirectCallBack); |
42 | 59 |
|
43 | 60 | function signIn() { |
44 | | - let loginRequest = { |
45 | | - scopes: applicationConfig.graphScopes |
46 | | - }; |
47 | | - myMSALObj.loginPopup(loginRequest).then(function (loginResponse) { |
| 61 | + |
| 62 | + myMSALObj.loginPopup(request).then(function (loginResponse) { |
48 | 63 | //Login Success |
49 | 64 | showWelcomeMessage(); |
50 | 65 | acquireTokenPopupAndCallMSGraph(); |
|
59 | 74 |
|
60 | 75 | function acquireTokenPopupAndCallMSGraph() { |
61 | 76 | //Call acquireTokenSilent (iframe) to obtain a token for Microsoft Graph |
62 | | - let tokenRequest = { |
63 | | - scopes: applicationConfig.graphScopes |
64 | | - }; |
65 | | - myMSALObj.acquireTokenSilent(tokenRequest).then(function (tokenResponse) { |
66 | | - console.log(tokenResponse.scopes); |
67 | | - callMSGraph(applicationConfig.graphEndpoint, tokenResponse.accessToken, graphAPICallback); |
| 77 | + |
| 78 | + myMSALObj.acquireTokenSilent(request).then(function (tokenResponse) { |
| 79 | + callMSGraph(graphConfig.graphMeEndpoint, tokenResponse.accessToken, graphAPICallback); |
68 | 80 | }).catch(function (error) { |
69 | | - console.log(error); |
| 81 | + console.log(error.errorCode); |
70 | 82 | // Call acquireTokenPopup (popup window) in case of acquireTokenSilent failure due to consent or interaction required ONLY |
71 | | - if (requiresInteraction(error.errorMessage)) { |
72 | | - myMSALObj.acquireTokenPopup(tokenRequest).then(function (tokenResponse) { |
73 | | - callMSGraph(applicationConfig.graphEndpoint, tokenResponse.accessToken, graphAPICallback); |
| 83 | + if (requiresInteraction(error.errorCode)) { |
| 84 | + myMSALObj.acquireTokenPopup(request).then(function (tokenResponse) { |
| 85 | + callMSGraph(graphConfig.graphMeEndpoint, tokenResponse.accessToken, graphAPICallback); |
74 | 86 | }).catch(function (error) { |
75 | 87 | console.log(error); |
76 | 88 | }); |
|
96 | 108 |
|
97 | 109 | function showWelcomeMessage() { |
98 | 110 | var divWelcome = document.getElementById('WelcomeMessage'); |
99 | | - divWelcome.innerHTML = 'Welcome ' + myMSALObj.getAccount().userName + "to Microsoft Graph API"; |
| 111 | + divWelcome.innerHTML = "Welcome " + myMSALObj.getAccount().userName + " to Microsoft Graph API"; |
100 | 112 | var loginbutton = document.getElementById('SignIn'); |
101 | 113 | loginbutton.innerHTML = 'Sign Out'; |
102 | 114 | loginbutton.setAttribute('onclick', 'signOut();'); |
103 | 115 | } |
104 | 116 |
|
105 | | - |
106 | 117 | // This function can be removed if you do not need to support IE |
107 | 118 | function acquireTokenRedirectAndCallMSGraph() { |
108 | 119 | //Call acquireTokenSilent (iframe) to obtain a token for Microsoft Graph |
109 | | - let tokenRequest = { |
110 | | - scopes: applicationConfig.graphScopes |
111 | | - }; |
112 | | - myMSALObj.acquireTokenSilent(tokenRequest).then(function (tokenResponse) { |
113 | | - callMSGraph(applicationConfig.graphEndpoint, tokenResponse.accessToken, graphAPICallback); |
| 120 | + myMSALObj.acquireTokenSilent(request).then(function (tokenResponse) { |
| 121 | + callMSGraph(graphConfig.graphMeEndpoint, tokenResponse.accessToken, graphAPICallback); |
114 | 122 | }).catch(function (error) { |
115 | 123 | console.log(error); |
116 | 124 | //Call acquireTokenRedirect in case of acquireToken Failure |
117 | | - if (requiresInteraction(error.errorMessage)) { |
118 | | - myMSALObj.acquireTokenRedirect(tokenRequest); |
| 125 | + if (requiresInteraction(error.errorCode)) { |
| 126 | + myMSALObj.acquireTokenRedirect(request); |
119 | 127 | } |
120 | 128 | }); |
121 | 129 | } |
122 | 130 |
|
123 | 131 | function acquireTokenRedirectCallBack(response) { |
124 | 132 | if (response.tokenType === "access_token") { |
125 | | - callMSGraph(applicationConfig.graphEndpoint, response.accessToken, graphAPICallback); |
| 133 | + callMSGraph(graphConfig.graphMeEndpoint, response.accessToken, graphAPICallback); |
126 | 134 | } else { |
127 | 135 | console.log("token type is:" + response.tokenType); |
128 | 136 | } |
|
132 | 140 | console.log(error); |
133 | 141 | } |
134 | 142 |
|
135 | | - function requiresInteraction(errorMessage) { |
136 | | - if (!errorMessage || !errorMessage.length) { |
| 143 | + function requiresInteraction(errorCode) { |
| 144 | + if (!errorCode || !errorCode.length) { |
137 | 145 | return false; |
138 | 146 | } |
139 | | - return errorMessage.indexOf("consent_required") !== -1 || |
140 | | - errorMessage.indexOf("interaction_required") !== -1 || |
141 | | - errorMessage.indexOf("login_required") !== -1; |
| 147 | + return errorCode === "consent_required" || |
| 148 | + errorCode === "interaction_required" || |
| 149 | + errorCode === "login_required"; |
142 | 150 | } |
143 | | - // Browser check variables |
144 | | - var ua = window.navigator.userAgent; |
145 | | - var msie = ua.indexOf('MSIE '); |
146 | | - var msie11 = ua.indexOf('Trident/'); |
147 | | - var msedge = ua.indexOf('Edge/'); |
148 | | - var isIE = msie > 0 || msie11 > 0; |
149 | | - var isEdge = msedge > 0; |
150 | 151 |
|
151 | | - //If you support IE, our recommendation is that you sign-in using Redirect APIs |
152 | | - //If you as a developer are testing using Edge InPrivate mode, please add "isEdge" to the if check |
153 | | - if (!isIE) { |
| 152 | + // runs on page load, change config to try different login types to see what is best for your application |
| 153 | + if (loginType === 'POPUP') { |
154 | 154 | if (myMSALObj.getAccount()) {// avoid duplicate code execution on page load in case of iframe and popup window. |
155 | 155 | showWelcomeMessage(); |
156 | 156 | acquireTokenPopupAndCallMSGraph(); |
157 | 157 | } |
158 | 158 | } |
159 | | - else { |
| 159 | + else if (loginType === 'REDIRECT') { |
160 | 160 | document.getElementById("SignIn").onclick = function () { |
161 | | - let redirectTokenRequest = { |
162 | | - scopes: applicationConfig.graphScopes |
163 | | - }; |
164 | | - myMSALObj.loginRedirect(redirectTokenRequest); |
| 161 | + myMSALObj.loginRedirect(request); |
165 | 162 | }; |
166 | 163 |
|
167 | 164 | if (myMSALObj.getAccount() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window. |
168 | 165 | showWelcomeMessage(); |
169 | 166 | acquireTokenRedirectAndCallMSGraph(); |
170 | 167 | } |
| 168 | + } else { |
| 169 | + console.error('Please set a valid login type'); |
171 | 170 | } |
172 | 171 | </script> |
173 | 172 | </body> |
|
0 commit comments