-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTenant.jsx
58 lines (50 loc) · 1.81 KB
/
Tenant.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { useEffect, useState } from 'react';
import { getSubscriptionClient } from '../azureManagement';
import { TenantData } from '../components/DataDisplay';
import { useMsal, useMsalAuthentication } from '@azure/msal-react';
import { InteractionType } from '@azure/msal-browser';
import { protectedResources } from '../authConfig';
export const Tenant = () => {
const { instance } = useMsal();
const [tenantInfo, setTenantInfo] = useState(null);
const account = instance.getActiveAccount();
const request = {
scopes: protectedResources.armTenants.scopes,
account: account,
};
const { login, result, error } = useMsalAuthentication(InteractionType.Popup, {
...request,
redirectUri: '/redirect',
});
const fetchData = async (accessToken) => {
const client = await getSubscriptionClient(accessToken);
const resArray = [];
for await (let item of client.tenants.list()) {
resArray.push(item);
}
setTenantInfo(resArray);
};
useEffect(() => {
if (!!tenantInfo) {
return;
}
if (!!error) {
// in case popup is blocked, use redirect instead
if (error.errorCode === 'popup_window_error' || error.errorCode === 'empty_window_error') {
login(InteractionType.Redirect, request);
}
console.log(error);
return;
}
if (result) {
fetchData(result.accessToken).catch((error) => {
console.log(error);
});
}
// eslint-disable-next-line
}, [tenantInfo, result, error, login]);
if (error) {
return <div>Error: {error.message}</div>;
}
return <>{tenantInfo ? <TenantData response={result} tenantInfo={tenantInfo} /> : null}</>;
};