Skip to content

Commit 8364e48

Browse files
committed
Address feedback
1 parent 67910cd commit 8364e48

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

articles/active-directory/develop/scenario-spa-acquire-token.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,20 @@ Alternatively, you can explicitly acquire tokens by using the acquire-token meth
166166
The following code combines the previously described pattern with the methods for a pop-up experience:
167167

168168
```javascript
169-
import { InteractionRequiredAuthError } from "@azure/msal-browser";
169+
import { InteractionRequiredAuthError, InteractionStatus } from "@azure/msal-browser";
170170
import { AuthenticatedTemplate, useMsal } from "@azure/msal-react";
171171

172172
function ProtectedComponent() {
173173
const { instance, inProgress, accounts } = useMsal();
174174
const [apiData, setApiData] = useState(null);
175175

176-
const accessTokenRequest = {
177-
scopes: ["user.read"],
178-
account: accounts[0]
179-
}
180176

181177
useEffect(() => {
182178
if (!apiData && inProgress === InteractionStatus.None) {
179+
const accessTokenRequest = {
180+
scopes: ["user.read"],
181+
account: accounts[0]
182+
}
183183
instance.acquireTokenSilent(accessTokenRequest).then((accessTokenResponse) => {
184184
// Acquire token silent success
185185
let accessToken = accessTokenResponse.accessToken;
@@ -200,7 +200,7 @@ function ProtectedComponent() {
200200
console.log(error);
201201
})
202202
}
203-
}, [instance, accessTokenRequest, inProgress, apiData]);
203+
}, [instance, accounts, inProgress, apiData]);
204204

205205
return <p>Return your protected content here: {apiData}</p>
206206
}
@@ -225,6 +225,7 @@ const accessTokenRequest = {
225225
account: account
226226
}
227227

228+
// Use the same publicClientApplication instance provided to MsalProvider
228229
publicClientApplication.acquireTokenSilent(accessTokenRequest).then(function(accessTokenResponse) {
229230
// Acquire token silent success
230231
let accessToken = accessTokenResponse.accessToken;
@@ -247,10 +248,10 @@ The following pattern is as described earlier but shown with a redirect method t
247248
```javascript
248249
const redirectResponse = await publicClientApplication.handleRedirectPromise();
249250
if (redirectResponse !== null) {
250-
// Acquire token silent success
251-
let accessToken = redirectResponse.accessToken;
252-
// Call your API with token
253-
callApi(accessToken);
251+
// Acquire token silent success
252+
let accessToken = redirectResponse.accessToken;
253+
// Call your API with token
254+
callApi(accessToken);
254255
} else {
255256
// MSAL.js v2 exposes several account APIs, logic to determine which account to use is the responsibility of the developer
256257
const account = publicClientApplication.getAllAccounts()[0];
@@ -343,19 +344,19 @@ This code is the same as described earlier.
343344
If `acquireTokenSilent` fails, fallback to `acquireTokenRedirect`. This method will initiate a full-frame redirect and the response will be handled when returning to the application. When this component is rendered after returning from the redirect, `acquireTokenSilent` should now succeed as the tokens will be pulled from the cache.
344345
345346
```javascript
346-
import { InteractionRequiredAuthError } from "@azure/msal-browser";
347+
import { InteractionRequiredAuthError, InteractionStatus } from "@azure/msal-browser";
347348
import { AuthenticatedTemplate, useMsal } from "@azure/msal-react";
348349

349350
function ProtectedComponent() {
350351
const { instance, inProgress, accounts } = useMsal();
351352
const [apiData, setApiData] = useState(null);
352353

353-
const accessTokenRequest = {
354-
scopes: ["user.read"],
355-
account: accounts[0]
356-
}
357354

358355
useEffect(() => {
356+
const accessTokenRequest = {
357+
scopes: ["user.read"],
358+
account: accounts[0]
359+
}
359360
if (!apiData && inProgress === InteractionStatus.None) {
360361
instance.acquireTokenSilent(accessTokenRequest).then((accessTokenResponse) => {
361362
// Acquire token silent success
@@ -369,7 +370,7 @@ function ProtectedComponent() {
369370
console.log(error);
370371
})
371372
}
372-
}, [instance, accessTokenRequest, inProgress, apiData]);
373+
}, [instance, accounts, inProgress, apiData]);
373374

374375
return <p>Return your protected content here: {apiData}</p>
375376
}
@@ -394,6 +395,7 @@ const accessTokenRequest = {
394395
account: account
395396
}
396397

398+
// Use the same publicClientApplication instance provided to MsalProvider
397399
publicClientApplication.acquireTokenSilent(accessTokenRequest).then(function(accessTokenResponse) {
398400
// Acquire token silent success
399401
let accessToken = accessTokenResponse.accessToken;

articles/active-directory/develop/scenario-spa-sign-in.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ function App() {
188188
You can also use the `@azure/msal-browser` APIs directly to invoke a login paired with the `AuthenticatedTemplate` and/or `UnauthenticatedTemplate` components to render specific contents to signed-in or signed-out users respectively. This is the recommended approach if you need to invoke login as a result of user interaction such as a button click.
189189

190190
```javascript
191-
import { useMsal } from "@azure/msal-react";
191+
import { useMsal, AuthenticatedTemplate, UnauthenticatedTemplate } from "@azure/msal-react";
192192

193193
function signInClickHandler(instance) {
194194
instance.loginPopup();
@@ -428,7 +428,7 @@ function SignOutButton() {
428428
// useMsal hook will return the PublicClientApplication instance you provided to MsalProvider
429429
const { instance } = useMsal();
430430

431-
return <button onClick={() => signOutClickHandler(instance)}>Sign In</button>
431+
return <button onClick={() => signOutClickHandler(instance)}>Sign Out</button>
432432
};
433433

434434
// Remember that MsalProvider must be rendered somewhere higher up in the component tree
@@ -529,7 +529,7 @@ function SignOutButton() {
529529
// useMsal hook will return the PublicClientApplication instance you provided to MsalProvider
530530
const { instance } = useMsal();
531531

532-
return <button onClick={() => signOutClickHandler(instance)}>Sign In</button>
532+
return <button onClick={() => signOutClickHandler(instance)}>Sign Out</button>
533533
};
534534

535535
// Remember that MsalProvider must be rendered somewhere higher up in the component tree

0 commit comments

Comments
 (0)