49 lines
2.2 KiB
JavaScript
49 lines
2.2 KiB
JavaScript
console.log("Keycloak Script running!");
|
|
const keycloak = new Keycloak({
|
|
realm: "schmoekerei",
|
|
"auth-server-url": "https://auth.xn--schmkerei-37a.de",
|
|
"ssl-required": "external",
|
|
resource: "account",
|
|
"public-client": true,
|
|
"confidential-port": 0,
|
|
url: "https://auth.xn--schmkerei-37a.de",
|
|
clientId: "homer",
|
|
"enable-cors": true,
|
|
});
|
|
// Creates logout URI based on updated requirements, see https://www.keycloak.org/2022/04/keycloak-1800-released
|
|
const createLogoutUrl = () => {
|
|
return keycloak.endpoints.logout()
|
|
+ '?id_token_hint=' + keycloak.idToken
|
|
+ '&post_logout_redirect_uri=' + encodeURIComponent(window.location.href);
|
|
}
|
|
const loadData = () => {
|
|
console.log(keycloak.subject);
|
|
if (keycloak.idToken) {
|
|
document.getElementById("keycloak-footer").innerHTML = '<i class="fas fa-sign-out-alt"></i> [' + keycloak.idTokenParsed.preferred_username + "] ausloggen";
|
|
document.getElementById("keycloak-footer").href = createLogoutUrl();
|
|
} else {
|
|
keycloak.loadUserProfile(
|
|
function () {
|
|
document.getElementById("keycloak-footer").innerHTML = '<i class="fas fa-sign-out-alt"></i> [' + keycloak.idTokenParsed.preferred_username + "] ausloggen";
|
|
document.getElementById("keycloak-footer").href = createLogoutUrl();
|
|
},
|
|
function () {
|
|
console.log("Failed to retrieve user details. Please enable claims or account role");
|
|
}
|
|
);
|
|
}
|
|
};
|
|
const loadFailure = () => {
|
|
console.log("Failed to load data. Check console log");
|
|
};
|
|
const reloadData = () => {
|
|
keycloak
|
|
.updateToken(10)
|
|
.then(loadData)
|
|
.catch(() => {
|
|
document.getElementById("keycloak-footer").innerHTML = '<i class="fas fa-sign-in-alt"></i> Login/Registrierung';
|
|
document.getElementById("keycloak-footer").href = "https://auth.xn--schmkerei-37a.de/realms/schmoekerei/protocol/openid-connect/auth?client_id=homer&response_type=code&redirect_uri=https://schmökerei.de";
|
|
});
|
|
};
|
|
keycloak.init({ onLoad: "check-sso", silentCheckSsoRedirectUri: "https://xn--schmkerei-37a.de/assets/silent-check-sso.html", silentCheckSsoFallback: false }).then(reloadData);
|