javascriptkeycloakjboss-tools

Loading Keycloak config from URL


The Keycloak 3.2 Javascript Adapter documentation says you can instantiate the keycloak object with :

var keycloak = Keycloak('http://localhost:8080/myapp/keycloak.json'));

But when I try to do so, it looks like my keycloak.config file is not propertly set as I end up with :

Uncaught TypeError: Cannot read property 'charAt' of undefined
  at getRealmUrl (keycloak.js:482)
  at Keycloak.kc.createLoginUrl (keycloak.js:233)
  at Object.login (keycloak.js:897)
  at Keycloak.kc.login (keycloak.js:205)
  at doLogin (keycloak.js:121)
  at onLoad (keycloak.js:144)
  at Object.processInit [as successCallback] (keycloak.js:187)
  at Object.setSuccess (keycloak.js:771)
  at XMLHttpRequest.req.onreadystatechange (keycloak.js:598)
  at XMLHttpRequest.wrapFn [as __zone_symbol___onreadystatechange] (zone.js:1056)

Yet, when I access http://localhost:8080/myapp/keycloak.json with my browser I get :

{
  url: 'http://keycloak-server/auth',
  realm: 'myrealm',
  clientId: 'myapp'
}

and if I instantiate the keycloak object with

var keycloak = Keycloak({
   url: 'http://keycloak-server/auth',
   realm: 'myrealm',
   clientId: 'myapp'
});

everything works fine.

Is this a Keycloak bug, or is there anything I'm missing ?


Solution

  • Keycloak uses a different format whether you load the config from its URL or straight into the constructor...

    If you load it via its URL, the format has to be :

    {
      auth-server-url: 'http://keycloak-server/auth',
      realm: 'myrealm',
      resource: 'myapp'
    }
    

    and not

    {
      url: 'http://keycloak-server/auth',
      realm: 'myrealm',
      clientId: 'myapp'
    }