Force token refresh

This commit is contained in:
Steve Faulkner 2021-01-07 14:13:41 -06:00
parent d1ac8eb077
commit 35213a77e2
2 changed files with 5 additions and 15 deletions

View File

@ -55,8 +55,6 @@ export const AccountSwitcher: React.FunctionComponent<Props> = ({ armToken, setD
const accounts = useDatabaseAccounts(selectedSubscriptionId, armToken); const accounts = useDatabaseAccounts(selectedSubscriptionId, armToken);
const [selectedAccountName, setSelectedAccoutName] = React.useState<string>(cachedDatabaseAccountName); const [selectedAccountName, setSelectedAccoutName] = React.useState<string>(cachedDatabaseAccountName);
console.log(subscriptions, accounts);
React.useEffect(() => { React.useEffect(() => {
if (accounts && selectedAccountName) { if (accounts && selectedAccountName) {
const account = accounts.find(account => account.name === selectedAccountName); const account = accounts.find(account => account.name === selectedAccountName);

View File

@ -2,7 +2,7 @@ import * as React from "react";
import { useBoolean } from "@uifabric/react-hooks"; import { useBoolean } from "@uifabric/react-hooks";
import { UserAgentApplication, Account } from "msal"; import { UserAgentApplication, Account } from "msal";
let msal = new UserAgentApplication({ const msal = new UserAgentApplication({
cache: { cache: {
cacheLocation: "localStorage" cacheLocation: "localStorage"
}, },
@ -52,35 +52,27 @@ export function useAADAuth(): ReturnType {
const switchTenant = React.useCallback( const switchTenant = React.useCallback(
async id => { async id => {
msal = new UserAgentApplication({
cache: {
cacheLocation: "localStorage"
},
auth: {
authority: `https://login.microsoftonline.com/${id}`,
clientId: "203f1145-856a-4232-83d4-a43568fba23d",
redirectUri: "https://dataexplorer-dev.azurewebsites.net" // TODO! This should only be set in development
}
});
const response = await msal.loginPopup({ const response = await msal.loginPopup({
authority: `https://login.microsoftonline.com/${id}` authority: `https://login.microsoftonline.com/${id}`
}); });
setTenantId(response.tenantId); setTenantId(response.tenantId);
setAccount(response.account); setAccount(response.account);
console.log(response);
}, },
[account, tenantId] [account, tenantId]
); );
React.useEffect(() => { React.useEffect(() => {
if (account && tenantId) { if (account && tenantId) {
console.log("Getting tokens for", tenantId);
Promise.all([ Promise.all([
msal.acquireTokenSilent({ msal.acquireTokenSilent({
// There is a bug in MSALv1 that requires us to refresh the token. Their internal cache is not respecting authority
forceRefresh: true,
authority: `https://login.microsoftonline.com/${tenantId}`, authority: `https://login.microsoftonline.com/${tenantId}`,
scopes: ["https://graph.windows.net//.default"] scopes: ["https://graph.windows.net//.default"]
}), }),
msal.acquireTokenSilent({ msal.acquireTokenSilent({
// There is a bug in MSALv1 that requires us to refresh the token. Their internal cache is not respecting authority
forceRefresh: true,
authority: `https://login.microsoftonline.com/${tenantId}`, authority: `https://login.microsoftonline.com/${tenantId}`,
scopes: ["https://management.azure.com//.default"] scopes: ["https://management.azure.com//.default"]
}) })