Skip to content
Snippets Groups Projects
Commit 6d9a66fd authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Handle keycloak init blocking forever

In case the login happens through a login iframe and the iframe src either doesn't
exist or it doesn't respond with a message then keycloak will block in init() forever.

Add a timeout so that after 5 seconds we abort the init and emit a changed event.
parent b36858f0
No related branches found
No related tags found
No related merge requests found
import {EventTarget} from "event-target-shim"; // Because EventTarget() doesn't exist on Safari import {EventTarget} from "event-target-shim"; // Because EventTarget() doesn't exist on Safari
const promiseTimeout = function(ms, promise) {
let timeout = new Promise((resolve, reject) => {
let id = setTimeout(() => {
clearTimeout(id);
reject('Timed out in '+ ms + 'ms.');
}, ms);
});
return Promise.race([
promise,
timeout
]);
};
/** /**
* Wraps the keycloak API to support async/await, adds auto token refreshing and consolidates all * Wraps the keycloak API to support async/await, adds auto token refreshing and consolidates all
* events into one native "changed" event * events into one native "changed" event
...@@ -83,9 +99,16 @@ export class KeycloakWrapper extends EventTarget { ...@@ -83,9 +99,16 @@ export class KeycloakWrapper extends EventTarget {
if (this._silentCheckSsoUri) { if (this._silentCheckSsoUri) {
options['onLoad'] = 'check-sso'; options['onLoad'] = 'check-sso';
options['silentCheckSsoRedirectUri'] = this._silentCheckSsoUri; options['silentCheckSsoRedirectUri'] = this._silentCheckSsoUri;
}
await this._keycloak.init(options); // When silent-sso-check is active but the iframe doesn't load/work we will
// never return here, so add a timeout and emit a signal so the app can continue
await promiseTimeout(5000, this._keycloak.init(options)).catch(() => {
console.log('Login timed out');
this._onChanged();
});
} else {
await this._keycloak.init(options);
}
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment