Allow four ways to set location of keycloak server. (#482)

* Allow four ways to set location of keycloak server.

* Make it work for devs on Keycloak 12 and below.
This commit is contained in:
Stan Silvert 2021-04-06 02:44:37 -04:00 committed by GitHub
parent f3dde7d2ee
commit 83a8f2baa7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,23 +6,46 @@ export default async function (): Promise<KcAdminClient> {
const kcAdminClient = new KcAdminClient(); const kcAdminClient = new KcAdminClient();
const authContext = "/auth";
const keycloakAuthUrl = window.location.origin + authContext;
const devMode = !window.location.pathname.startsWith("/adminv2");
try { try {
await kcAdminClient.init( await kcAdminClient.init(
{ onLoad: "check-sso", pkceMethod: "S256" }, { onLoad: "check-sso", pkceMethod: "S256" },
{ {
url: devMode ? "http://localhost:8180/auth" : keycloakAuthUrl, url: keycloakAuthUrl(),
realm: realm, realm: realm,
clientId: "security-admin-console-v2", clientId: "security-admin-console-v2",
} }
); );
kcAdminClient.setConfig({ realmName: realm }); kcAdminClient.setConfig({ realmName: realm });
kcAdminClient.baseUrl = authContext;
// we can get rid of devMode once developers upgrade to Keycloak 13
const devMode = !window.location.pathname.startsWith("/adminv2");
kcAdminClient.baseUrl = devMode ? "/auth" : keycloakAuthUrl();
} catch (error) { } catch (error) {
alert("failed to initialize keycloak"); alert("failed to initialize keycloak");
} }
return kcAdminClient; return kcAdminClient;
} }
const keycloakAuthUrl = () => {
// Eventually, authContext should not be hard-coded.
// You are allowed to change this context on your keycloak server,
// but it is rarely done.
const authContext = "/auth";
const searchParams = new URLSearchParams(window.location.search);
// passed in as query param
const authUrlFromParam = searchParams.get("keycloak-server");
if (authUrlFromParam) return authUrlFromParam + authContext;
// dev mode
if (!window.location.pathname.startsWith("/adminv2"))
return "http://localhost:8180" + authContext;
// demo mode
if (searchParams.get("demo")) return "http://localhost:8080" + authContext;
// admin console served from keycloak server
return window.location.origin + authContext;
};