This commit is contained in:
Steve Faulkner 2021-01-05 01:14:17 -06:00
parent d9156c47d0
commit d1ac8eb077
4 changed files with 39 additions and 21 deletions

View File

@ -31,7 +31,7 @@ const App: React.FunctionComponent = () => {
// For showing/hiding panel // For showing/hiding panel
const [isOpen, { setTrue: openPanel, setFalse: dismissPanel }] = useBoolean(false); const [isOpen, { setTrue: openPanel, setFalse: dismissPanel }] = useBoolean(false);
const { isLoggedIn, armToken, graphToken, account, tenantId, logout, login } = useAADAuth(); const { isLoggedIn, armToken, graphToken, account, tenantId, logout, login, switchTenant } = useAADAuth();
const [databaseAccount, setDatabaseAccount] = React.useState<DatabaseAccount>(); const [databaseAccount, setDatabaseAccount] = React.useState<DatabaseAccount>();
const [authType, setAuthType] = React.useState<AuthType>(encryptedToken ? AuthType.EncryptedToken : undefined); const [authType, setAuthType] = React.useState<AuthType>(encryptedToken ? AuthType.EncryptedToken : undefined);
const [connectionString, setConnectionString] = React.useState<string>(); const [connectionString, setConnectionString] = React.useState<string>();
@ -132,7 +132,7 @@ const App: React.FunctionComponent = () => {
{!isLoggedIn && !encryptedTokenMetadata && ( {!isLoggedIn && !encryptedTokenMetadata && (
<ConnectExplorer {...{ login, setEncryptedToken, setAuthType, connectionString, setConnectionString }} /> <ConnectExplorer {...{ login, setEncryptedToken, setAuthType, connectionString, setConnectionString }} />
)} )}
{isLoggedIn && <DirectoryPickerPanel {...{ isOpen, dismissPanel, armToken, tenantId }} />} {isLoggedIn && <DirectoryPickerPanel {...{ isOpen, dismissPanel, armToken, tenantId, switchTenant }} />}
</> </>
); );
}; };

View File

@ -55,6 +55,8 @@ 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

@ -7,13 +7,15 @@ interface Props {
dismissPanel: () => void; dismissPanel: () => void;
tenantId: string; tenantId: string;
armToken: string; armToken: string;
switchTenant: (tenantId: string) => void;
} }
export const DirectoryPickerPanel: React.FunctionComponent<Props> = ({ export const DirectoryPickerPanel: React.FunctionComponent<Props> = ({
isOpen, isOpen,
dismissPanel, dismissPanel,
armToken, armToken,
tenantId tenantId,
switchTenant
}: Props) => { }: Props) => {
const directories = useDirectories(armToken); const directories = useDirectories(armToken);
return ( return (
@ -27,22 +29,9 @@ export const DirectoryPickerPanel: React.FunctionComponent<Props> = ({
<ChoiceGroup <ChoiceGroup
options={directories.map(dir => ({ key: dir.tenantId, text: `${dir.displayName} (${dir.tenantId})` }))} options={directories.map(dir => ({ key: dir.tenantId, text: `${dir.displayName} (${dir.tenantId})` }))}
selectedKey={tenantId} selectedKey={tenantId}
onChange={async () => { onChange={async (event, option) => {
switchTenant(option.key);
dismissPanel(); dismissPanel();
// TODO!!! Switching directories does not work. Still not sure why. Tried lots of stuff.
// const response = await msal.loginPopup({
// authority: `https://login.microsoftonline.com/${option.key}`
// });
// // msal = new Msal.UserAgentApplication({
// // auth: {
// // authority: `https://login.microsoftonline.com/${option.key}`,
// // clientId: "203f1145-856a-4232-83d4-a43568fba23d",
// // redirectUri: "https://dataexplorer-dev.azurewebsites.net" // TODO! This should only be set in development
// // }
// // });
// setTenantId(option.key);
// setAccount(response.account);
// console.log(account);
}} }}
/> />
</Panel> </Panel>

View File

@ -2,12 +2,12 @@ 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";
const msal = new UserAgentApplication({ let msal = new UserAgentApplication({
cache: { cache: {
cacheLocation: "localStorage" cacheLocation: "localStorage"
}, },
auth: { auth: {
authority: "https://login.microsoft.com/common", authority: "https://login.microsoftonline.com/common",
clientId: "203f1145-856a-4232-83d4-a43568fba23d", clientId: "203f1145-856a-4232-83d4-a43568fba23d",
redirectUri: "https://dataexplorer-dev.azurewebsites.net" // TODO! This should only be set in development redirectUri: "https://dataexplorer-dev.azurewebsites.net" // TODO! This should only be set in development
} }
@ -24,6 +24,7 @@ interface ReturnType {
logout: () => void; logout: () => void;
tenantId: string; tenantId: string;
account: Account; account: Account;
switchTenant: (tenantId: string) => void;
} }
export function useAADAuth(): ReturnType { export function useAADAuth(): ReturnType {
@ -49,13 +50,38 @@ export function useAADAuth(): ReturnType {
msal.logout(); msal.logout();
}, []); }, []);
const switchTenant = React.useCallback(
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({
authority: `https://login.microsoftonline.com/${id}`
});
setTenantId(response.tenantId);
setAccount(response.account);
console.log(response);
},
[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({
authority: `https://login.microsoftonline.com/${tenantId}`,
scopes: ["https://graph.windows.net//.default"] scopes: ["https://graph.windows.net//.default"]
}), }),
msal.acquireTokenSilent({ msal.acquireTokenSilent({
authority: `https://login.microsoftonline.com/${tenantId}`,
scopes: ["https://management.azure.com//.default"] scopes: ["https://management.azure.com//.default"]
}) })
]).then(([graphTokenResponse, armTokenResponse]) => { ]).then(([graphTokenResponse, armTokenResponse]) => {
@ -72,6 +98,7 @@ export function useAADAuth(): ReturnType {
graphToken, graphToken,
armToken, armToken,
login, login,
logout logout,
switchTenant
}; };
} }