question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ERROR An error happened during Keycloak initialization

See original GitHub issue

I’m trying to use

try {
  await keycloak.init({
    config: {
      url: 'https://xxxx.com/auth',
      realm: 'xxxxx',
      clientId: 'account',
      credentials: {
        secret: 'xxxxxx-980a-4588-a846-xxxxxxx'
      }
    },
    initOptions: {
      onLoad: 'login-required',
      checkLoginIframe: false
    },
    bearerExcludedUrls: [
      '/assets'
    ]
  });
  resolve();
} catch (error) {
  reject(error);
}

in the browser see this error:

ERROR An error happened during Keycloak initialization.         core.js:1448
defaultErrorLogger                    core.js:1448
ErrorHandler.prototype.handleError    core.js:1509
_callAndReportToErrorHandler/</<      core.js:5682:54
ZoneDelegate.prototype.invoke         zone.js:388
Zone.prototype.run                    zone.js:138
NgZone.prototype.runOutsideAngular    core.js:4697:47
_callAndReportToErrorHandler/<        core.js:5682
ZoneDelegate.prototype.invoke         zone.js:388
onInvoke                              core.js:4749
ZoneDelegate.prototype.invoke         zone.js:387
Zone.prototype.run                    zone.js:138
scheduleResolveOrReject/<             zone.js:858
ZoneDelegate.prototype.invokeTask     zone.js:421
onInvokeTask                          core.js:4740
ZoneDelegate.prototype.invokeTask     zone.js:420
Zone.prototype.runTask                zone.js:188
drainMicroTaskQueue                   zone.js:595
ZoneTask.invokeTask                   zone.js:500
invokeTask                            zone.js:1517
globalZoneAwareCallback               zone.js:1543

also:

Pedido de origen cruzado bloqueado: La política de mismo origen no permite leer el recurso remoto en https://xxxxx.com/auth/realms/xxxx/protocol/openid-connect/token. (Razón: encabezado CORS 'Access-Control-Allow-Origin' faltante).

this causes that the web does not load.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
sateliermartincommented, Mar 13, 2018

I found out what the error is.

The error is CORS ‘Access-Control-Allow-Origin’.

To test it, disable Cross Source Restriction in the Safari browser and it worked correctly.

0reactions
sateliermartincommented, Mar 13, 2018

The configuration of keycloak is standard, do not make any special configuration and the clientId and secret are correct.

analyzing the code, go to line 546 of keycloak.js:

if ((kc.flow != 'implicit') && code) {
    var params = 'code=' + code + '&grant_type=authorization_code';
    var url = getRealmUrl() + '/protocol/openid-connect/token';

    var req = new XMLHttpRequest();
    req.open('POST', url, true);
    req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

    if (kc.clientId && kc.clientSecret) {
        req.setRequestHeader('Authorization', 'Basic ' + btoa(kc.clientId + ':' + kc.clientSecret));
    } else {
        params += '&client_id=' + encodeURIComponent(kc.clientId);
    }

    params += '&redirect_uri=' + oauth.redirectUri;

    req.withCredentials = true;

    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if (req.status == 200) {

                var tokenResponse = JSON.parse(req.responseText);
                authSuccess(tokenResponse['access_token'], tokenResponse['refresh_token'], tokenResponse['id_token'], kc.flow === 'standard');
            } else {
                kc.onAuthError && kc.onAuthError();
                promise && promise.setError();
            }
        }
    };

    req.send(params);
}

req.status is 0

Any other idea to be able to find the problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

An error happened during Keycloak initialization
Only in Firefox web browser I am getting the below error message: Error: "Uncaught (in promise): An error happened during Keycloak ...
Read more >
The account console presents "Failed to initialize keycloak ...
I am new to keycloak and struggling with the same ““failed to initatilize keycloak” issue as others. But the workaround described here (” ......
Read more >
keycloak.init's initPromise isn't resolved when init fails - Red Hat
I can confirm this is happening if the keycloak server is down or doesn't exist. There is no error thrown, it seems to...
Read more >
KEYCLOAK-16455 Solution to silent fails of JS adapter
Hello all,. Currently, init function in Keycloak JS adapter silently fails when Keycloak URL is unavailable or realm is wrongly configured.
Read more >
keycloak-angular - UNPKG
The CDN for keycloak-angular. ... }\n resolve(authenticated);\n })\n .error(kcError => {\n let msg = 'An error happened during Keycloak initialization.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found