KEYCLOAK-15158 adjust basic example of Keycloak JS adapter usage

As the JS adapter has evolved, it now tries to add an iframe element to the document body during initialization.
When used like in the basic example as before, there will be an JS error, as the document body is not yet available.
So we've to wait until the document is loaded, thus the initialization is put in a function and the function will be called upon body.onload()
This commit is contained in:
Niko Köbler 2020-09-11 10:20:07 +02:00 committed by Stian Thorgersen
parent 8ff235e630
commit f9e5d78a56

View file

@ -24,17 +24,24 @@ The following example shows how to initialize the JavaScript adapter:
[source,html] [source,html]
---- ----
<html>
<head> <head>
<script src="keycloak.js"></script> <script src="keycloak.js"></script>
<script> <script>
var keycloak = new Keycloak(); function initKeycloak() {
keycloak.init().then(function(authenticated) { var keycloak = new Keycloak();
alert(authenticated ? 'authenticated' : 'not authenticated'); keycloak.init().then(function(authenticated) {
}).catch(function() { alert(authenticated ? 'authenticated' : 'not authenticated');
alert('failed to initialize'); }).catch(function() {
}); alert('failed to initialize');
});
}
</script> </script>
</head> </head>
<body onload="initKeycloak()">
<!-- your page content goes here -->
</body>
</html>
---- ----
If the `keycloak.json` file is in a different location you can specify it: If the `keycloak.json` file is in a different location you can specify it: