|
17 | 17 | </div> |
18 | 18 | </div> |
19 | 19 | <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 | | - |
34 | 20 | var msalConfig = { |
35 | 21 | auth: { |
36 | 22 | 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 |
38 | 24 | }, |
39 | 25 | cache: { |
40 | 26 | cacheLocation: "localStorage", |
|
46 | 32 | graphMeEndpoint: "https://graph.microsoft.com/v1.0/me" |
47 | 33 | }; |
48 | 34 |
|
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 = { |
52 | 38 | scopes: ["user.read"] |
53 | 39 | }; |
54 | 40 |
|
55 | 41 | var myMSALObj = new Msal.UserAgentApplication(msalConfig); |
| 42 | + |
56 | 43 | // Register Callbacks for redirect flow |
57 | 44 | myMSALObj.handleRedirectCallbacks(acquireTokenRedirectCallBack, acquireTokenErrorRedirectCallBack); |
58 | 45 |
|
59 | 46 | function signIn() { |
60 | | - |
61 | | - myMSALObj.loginPopup(request).then(function (loginResponse) { |
62 | | - //Login Success |
| 47 | + myMSALObj.loginPopup(requestObj).then(function (loginResponse) { |
| 48 | + //Successful login |
63 | 49 | showWelcomeMessage(); |
| 50 | + //Call MS Graph using the token in the response |
64 | 51 | acquireTokenPopupAndCallMSGraph(); |
65 | 52 | }).catch(function (error) { |
| 53 | + //Please check the console for errors |
66 | 54 | console.log(error); |
67 | 55 | }); |
68 | 56 | } |
|
72 | 60 | } |
73 | 61 |
|
74 | 62 | 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) { |
78 | 65 | callMSGraph(graphConfig.graphMeEndpoint, tokenResponse.accessToken, graphAPICallback); |
79 | 66 | }).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) |
82 | 70 | if (requiresInteraction(error.errorCode)) { |
83 | | - myMSALObj.acquireTokenPopup(request).then(function (tokenResponse) { |
| 71 | + myMSALObj.acquireTokenPopup(requestObj).then(function (tokenResponse) { |
84 | 72 | callMSGraph(graphConfig.graphMeEndpoint, tokenResponse.accessToken, graphAPICallback); |
85 | 73 | }).catch(function (error) { |
86 | 74 | console.log(error); |
|
100 | 88 | xmlHttp.send(); |
101 | 89 | } |
102 | 90 |
|
103 | | - |
104 | 91 | function graphAPICallback(data) { |
105 | 92 | document.getElementById("json").innerHTML = JSON.stringify(data, null, 2); |
106 | 93 | } |
|
113 | 100 | loginbutton.setAttribute('onclick', 'signOut();'); |
114 | 101 | } |
115 | 102 |
|
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 |
117 | 104 | 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) { |
120 | 107 | callMSGraph(graphConfig.graphMeEndpoint, tokenResponse.accessToken, graphAPICallback); |
121 | 108 | }).catch(function (error) { |
122 | 109 | 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 |
124 | 112 | if (requiresInteraction(error.errorCode)) { |
125 | | - myMSALObj.acquireTokenRedirect(request); |
| 113 | + myMSALObj.acquireTokenRedirect(requestObj); |
126 | 114 | } |
127 | 115 | }); |
128 | 116 | } |
|
148 | 136 | errorCode === "login_required"; |
149 | 137 | } |
150 | 138 |
|
| 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 | + |
151 | 153 | // runs on page load, change config to try different login types to see what is best for your application |
152 | 154 | if (loginType === 'POPUP') { |
153 | 155 | if (myMSALObj.getAccount()) {// avoid duplicate code execution on page load in case of iframe and popup window. |
|
157 | 159 | } |
158 | 160 | else if (loginType === 'REDIRECT') { |
159 | 161 | document.getElementById("SignIn").onclick = function () { |
160 | | - myMSALObj.loginRedirect(request); |
| 162 | + myMSALObj.loginRedirect(requestObj); |
161 | 163 | }; |
162 | 164 |
|
163 | 165 | 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