From 731999c4e87cc871e26494bea047789a7a5d81f0 Mon Sep 17 00:00:00 2001 From: Steve Faulkner Date: Sat, 2 Jan 2021 19:19:19 -0600 Subject: [PATCH] WIP --- src/DirectoryPickerPanel.tsx | 2 +- src/HostedExplorer.tsx | 59 +++--------------------------------- 2 files changed, 5 insertions(+), 56 deletions(-) diff --git a/src/DirectoryPickerPanel.tsx b/src/DirectoryPickerPanel.tsx index f8b3e988b..2e91d976e 100644 --- a/src/DirectoryPickerPanel.tsx +++ b/src/DirectoryPickerPanel.tsx @@ -29,7 +29,7 @@ export const DirectoryPickerPanel: React.FunctionComponent = ({ selectedKey={tenantId} onChange={async () => { dismissPanel(); - // TODO!!! This does not work. Still not sure why. Tried lots of stuff. + // 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}` // }); diff --git a/src/HostedExplorer.tsx b/src/HostedExplorer.tsx index f750613e9..6a0ccb013 100644 --- a/src/HostedExplorer.tsx +++ b/src/HostedExplorer.tsx @@ -1,5 +1,4 @@ import { useBoolean } from "@uifabric/react-hooks"; -import * as Msal from "msal"; import { initializeIcons } from "office-ui-fabric-react"; import * as React from "react"; import { render } from "react-dom"; @@ -17,80 +16,30 @@ import { MeControl } from "./MeControl"; import "./Platform/Hosted/ConnectScreen.less"; import "./Shared/appInsights"; import { SignInButton } from "./SignInButton"; +import { useAADAuth } from "./hooks/useAADAuth"; initializeIcons(); -const msal = new Msal.UserAgentApplication({ - cache: { - cacheLocation: "localStorage" - }, - auth: { - authority: "https://login.microsoft.com/common", - clientId: "203f1145-856a-4232-83d4-a43568fba23d", - redirectUri: "https://dataexplorer-dev.azurewebsites.net" // TODO! This should only be set in development - } -}); - interface HostedExplorerChildFrame extends Window { authType: AuthType; databaseAccount: DatabaseAccount; authorizationToken: string; } -const cachedAccount = msal.getAllAccounts()?.[0]; -const cachedTenantId = localStorage.getItem("cachedTenantId"); - const App: React.FunctionComponent = () => { - // Hooks for handling encrypted portal tokens + // For handling encrypted portal tokens sent via query paramter const params = new URLSearchParams(window.location.search); const [encryptedToken, setEncryptedToken] = React.useState(params && params.get("key")); const encryptedTokenMetadata = usePortalAccessToken(encryptedToken); - // Hooks for showing/hiding panel + // For showing/hiding panel const [isOpen, { setTrue: openPanel, setFalse: dismissPanel }] = useBoolean(false); - // Hooks for AAD authentication - const [isLoggedIn, { setTrue: setLoggedIn, setFalse: setLoggedOut }] = useBoolean( - Boolean(cachedAccount && cachedTenantId) || false - ); - const [account, setAccount] = React.useState(cachedAccount); - const [tenantId, setTenantId] = React.useState(cachedTenantId); - const [graphToken, setGraphToken] = React.useState(); - const [armToken, setArmToken] = React.useState(); + const { isLoggedIn, armToken, graphToken, account, tenantId, logout, login } = useAADAuth(); const [databaseAccount, setDatabaseAccount] = React.useState(); - const login = React.useCallback(async () => { - const response = await msal.loginPopup(); - setLoggedIn(); - setAccount(response.account); - setTenantId(response.tenantId); - localStorage.setItem("cachedTenantId", response.tenantId); - }, []); - - const logout = React.useCallback(() => { - setLoggedOut(); - localStorage.removeItem("cachedTenantId"); - msal.logout(); - }, []); - const ref = React.useRef(); - React.useEffect(() => { - if (account && tenantId) { - Promise.all([ - msal.acquireTokenSilent({ - scopes: ["https://graph.windows.net//.default"] - }), - msal.acquireTokenSilent({ - scopes: ["https://management.azure.com//.default"] - }) - ]).then(([graphTokenResponse, armTokenResponse]) => { - setGraphToken(graphTokenResponse.accessToken); - setArmToken(armTokenResponse.accessToken); - }); - } - }, [account, tenantId]); - React.useEffect(() => { // If ref.current is undefined no iframe has been rendered if (ref.current) {