-
Notifications
You must be signed in to change notification settings - Fork 0
/
kc-client.js
50 lines (44 loc) · 1.32 KB
/
kc-client.js
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
(function () {
const config = {
realm: "test-realm",
clientId: "test-client",
scope: "web-app",
url: "https://localhost:8443",
"ssl-required": "external",
"public-client": true,
"confidential-port": 0
};
const keycloak = new Keycloak(config);
keycloak
.init({
onLoad: "login-required",
promiseType: "native"
})
.then(function (authenticated) {
const userInfoElement = document.getElementById("userInfo");
const authStatusElement = document.getElementById("authStatus");
if (authenticated) {
authStatusElement.textContent = "Authenticated!";
const username = keycloak.tokenParsed.preferred_username;
const email = keycloak.tokenParsed.email;
userInfoElement.innerHTML = `
<p>Username: ${username}</p>
<p>Email: ${email}</p>
`;
} else {
authStatusElement.textContent = "Not Authenticated";
userInfoElement.textContent = "";
}
const logoutBtn = document.querySelector(".logout-button");
if (logoutBtn) {
logoutBtn.style.display = 'block'
logoutBtn.addEventListener("click", function () {
keycloak.logout();
});
}
})
.catch(function (error) {
console.error(error);
alert("failed to initialize");
});
})();