2022-04-27 15:29:57 +00:00
|
|
|
import KeycloakAdminClient from "@keycloak/keycloak-admin-client";
|
2021-04-29 06:28:59 +00:00
|
|
|
import axios from "axios";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { createContext, DependencyList, useEffect } from "react";
|
2021-04-29 06:28:59 +00:00
|
|
|
import { useErrorHandler } from "react-error-boundary";
|
2022-04-27 15:29:57 +00:00
|
|
|
|
|
|
|
import environment from "../../environment";
|
2021-07-21 09:30:18 +00:00
|
|
|
import useRequiredContext from "../../utils/useRequiredContext";
|
2020-11-12 12:55:52 +00:00
|
|
|
|
|
|
|
export const AdminClient = createContext<KeycloakAdminClient | undefined>(
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
|
2021-07-21 09:30:18 +00:00
|
|
|
export const useAdminClient = () => useRequiredContext(AdminClient);
|
2021-01-05 13:39:27 +00:00
|
|
|
|
2021-01-11 18:56:19 +00:00
|
|
|
/**
|
|
|
|
* Util function to only set the state when the component is still mounted.
|
|
|
|
*
|
|
|
|
* It takes 2 functions one you do your adminClient call in and the other to set your state
|
|
|
|
*
|
|
|
|
* @example
|
2021-04-29 06:28:59 +00:00
|
|
|
* useFetch(
|
|
|
|
* () => adminClient.components.findOne({ id }),
|
|
|
|
* (component) => setupForm(component),
|
|
|
|
* []
|
|
|
|
* );
|
2021-01-11 18:56:19 +00:00
|
|
|
*
|
|
|
|
* @param adminClientCall use this to do your adminClient call
|
|
|
|
* @param callback when the data is fetched this is where you set your state
|
|
|
|
*/
|
2021-04-29 06:28:59 +00:00
|
|
|
export function useFetch<T>(
|
2021-01-05 13:39:27 +00:00
|
|
|
adminClientCall: () => Promise<T>,
|
2021-02-17 21:12:25 +00:00
|
|
|
callback: (param: T) => void,
|
2021-04-29 06:28:59 +00:00
|
|
|
deps?: DependencyList
|
2021-01-05 13:39:27 +00:00
|
|
|
) {
|
2021-04-29 06:28:59 +00:00
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const onError = useErrorHandler();
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-11-18 12:55:21 +00:00
|
|
|
const source = axios.CancelToken.source();
|
|
|
|
|
|
|
|
adminClient.setConfig({
|
|
|
|
requestConfig: { cancelToken: source.token },
|
|
|
|
});
|
|
|
|
|
2021-04-29 06:28:59 +00:00
|
|
|
adminClientCall()
|
|
|
|
.then((result) => {
|
2021-06-16 11:35:03 +00:00
|
|
|
if (!source.token.reason) {
|
|
|
|
callback(result);
|
|
|
|
}
|
2021-04-29 06:28:59 +00:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
if (!axios.isCancel(error)) {
|
|
|
|
onError(error);
|
2021-02-17 21:12:25 +00:00
|
|
|
}
|
2021-04-29 06:28:59 +00:00
|
|
|
});
|
2021-01-05 13:39:27 +00:00
|
|
|
|
2021-11-18 12:55:21 +00:00
|
|
|
adminClient.setConfig({
|
|
|
|
requestConfig: { cancelToken: undefined },
|
|
|
|
});
|
|
|
|
|
2021-04-29 06:28:59 +00:00
|
|
|
return () => {
|
|
|
|
source.cancel();
|
|
|
|
};
|
|
|
|
}, deps);
|
2021-01-05 13:39:27 +00:00
|
|
|
}
|
2022-04-27 15:29:57 +00:00
|
|
|
|
|
|
|
export async function initAdminClient() {
|
|
|
|
const kcAdminClient = new KeycloakAdminClient();
|
|
|
|
|
|
|
|
await kcAdminClient.init(
|
|
|
|
{ onLoad: "check-sso", pkceMethod: "S256" },
|
|
|
|
{
|
|
|
|
url: environment.authUrl,
|
|
|
|
realm: environment.loginRealm,
|
|
|
|
clientId: environment.isRunningAsTheme
|
|
|
|
? "security-admin-console"
|
|
|
|
: "security-admin-console-v2",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
kcAdminClient.setConfig({ realmName: environment.loginRealm });
|
|
|
|
kcAdminClient.baseUrl = environment.authUrl;
|
|
|
|
|
|
|
|
return kcAdminClient;
|
|
|
|
}
|