2021-05-17 19:10:54 +01:00
|
|
|
import * as msal from "@azure/msal-browser";
|
2021-05-06 00:26:03 +01:00
|
|
|
import { useBoolean } from "@fluentui/react-hooks";
|
2021-05-17 19:10:54 +01:00
|
|
|
import * as React from "react";
|
2021-05-28 04:13:18 +01:00
|
|
|
import { configContext } from "../ConfigContext";
|
2021-05-27 21:18:44 +01:00
|
|
|
import { getMsalInstance } from "../Utils/AuthorizationUtils";
|
2021-01-19 22:31:55 +00:00
|
|
|
|
2021-05-27 21:18:44 +01:00
|
|
|
const msalInstance = getMsalInstance();
|
2021-01-19 22:31:55 +00:00
|
|
|
|
2021-05-17 19:10:54 +01:00
|
|
|
const cachedAccount = msalInstance.getAllAccounts()?.[0];
|
2021-01-19 22:31:55 +00:00
|
|
|
const cachedTenantId = localStorage.getItem("cachedTenantId");
|
|
|
|
|
|
|
|
interface ReturnType {
|
|
|
|
isLoggedIn: boolean;
|
|
|
|
graphToken: string;
|
|
|
|
armToken: string;
|
|
|
|
login: () => void;
|
|
|
|
logout: () => void;
|
|
|
|
tenantId: string;
|
2021-05-17 19:10:54 +01:00
|
|
|
account: msal.AccountInfo;
|
2021-01-19 22:31:55 +00:00
|
|
|
switchTenant: (tenantId: string) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useAADAuth(): ReturnType {
|
|
|
|
const [isLoggedIn, { setTrue: setLoggedIn, setFalse: setLoggedOut }] = useBoolean(
|
|
|
|
Boolean(cachedAccount && cachedTenantId) || false
|
|
|
|
);
|
2021-05-17 19:10:54 +01:00
|
|
|
const [account, setAccount] = React.useState<msal.AccountInfo>(cachedAccount);
|
2021-01-19 22:31:55 +00:00
|
|
|
const [tenantId, setTenantId] = React.useState<string>(cachedTenantId);
|
|
|
|
const [graphToken, setGraphToken] = React.useState<string>();
|
|
|
|
const [armToken, setArmToken] = React.useState<string>();
|
|
|
|
|
2021-05-17 19:10:54 +01:00
|
|
|
msalInstance.setActiveAccount(account);
|
2021-01-19 22:31:55 +00:00
|
|
|
const login = React.useCallback(async () => {
|
2021-05-28 04:13:18 +01:00
|
|
|
const response = await msalInstance.loginPopup({
|
|
|
|
redirectUri: configContext.msalRedirectURI,
|
|
|
|
scopes: [],
|
|
|
|
});
|
2021-01-19 22:31:55 +00:00
|
|
|
setLoggedIn();
|
|
|
|
setAccount(response.account);
|
|
|
|
setTenantId(response.tenantId);
|
|
|
|
localStorage.setItem("cachedTenantId", response.tenantId);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const logout = React.useCallback(() => {
|
|
|
|
setLoggedOut();
|
|
|
|
localStorage.removeItem("cachedTenantId");
|
2021-05-17 19:10:54 +01:00
|
|
|
msalInstance.logoutRedirect();
|
2021-01-19 22:31:55 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const switchTenant = React.useCallback(
|
2021-01-20 15:15:01 +00:00
|
|
|
async (id) => {
|
2021-05-17 19:10:54 +01:00
|
|
|
const response = await msalInstance.loginPopup({
|
2021-05-28 04:13:18 +01:00
|
|
|
redirectUri: configContext.msalRedirectURI,
|
2021-06-16 15:13:11 +01:00
|
|
|
authority: `${configContext.AAD_ENDPOINT}${id}`,
|
2021-05-17 19:10:54 +01:00
|
|
|
scopes: [],
|
2021-01-19 22:31:55 +00:00
|
|
|
});
|
|
|
|
setTenantId(response.tenantId);
|
|
|
|
setAccount(response.account);
|
2021-08-30 20:21:32 +01:00
|
|
|
localStorage.setItem("cachedTenantId", response.tenantId);
|
2021-01-19 22:31:55 +00:00
|
|
|
},
|
|
|
|
[account, tenantId]
|
|
|
|
);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (account && tenantId) {
|
|
|
|
Promise.all([
|
2021-05-17 19:10:54 +01:00
|
|
|
msalInstance.acquireTokenSilent({
|
2021-06-16 15:13:11 +01:00
|
|
|
authority: `${configContext.AAD_ENDPOINT}${tenantId}`,
|
|
|
|
scopes: [`${configContext.GRAPH_ENDPOINT}/.default`],
|
2021-01-19 22:31:55 +00:00
|
|
|
}),
|
2021-05-17 19:10:54 +01:00
|
|
|
msalInstance.acquireTokenSilent({
|
2021-06-16 15:13:11 +01:00
|
|
|
authority: `${configContext.AAD_ENDPOINT}${tenantId}`,
|
|
|
|
scopes: [`${configContext.ARM_ENDPOINT}/.default`],
|
2021-01-20 15:15:01 +00:00
|
|
|
}),
|
2021-05-27 21:18:44 +01:00
|
|
|
]).then(([graphTokenResponse, armTokenResponse]) => {
|
2021-01-19 22:31:55 +00:00
|
|
|
setGraphToken(graphTokenResponse.accessToken);
|
|
|
|
setArmToken(armTokenResponse.accessToken);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [account, tenantId]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
account,
|
|
|
|
tenantId,
|
|
|
|
isLoggedIn,
|
|
|
|
graphToken,
|
|
|
|
armToken,
|
|
|
|
login,
|
|
|
|
logout,
|
2021-01-20 15:15:01 +00:00
|
|
|
switchTenant,
|
2021-01-19 22:31:55 +00:00
|
|
|
};
|
|
|
|
}
|