From 6d9a66fdf546bc40000894a9a4244592e1d1ad41 Mon Sep 17 00:00:00 2001 From: Christoph Reiter <reiter.christoph@gmail.com> Date: Tue, 17 Dec 2019 12:14:09 +0100 Subject: [PATCH] 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. --- packages/auth/src/keycloak.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/auth/src/keycloak.js b/packages/auth/src/keycloak.js index bd2e53d1..2d66567b 100644 --- a/packages/auth/src/keycloak.js +++ b/packages/auth/src/keycloak.js @@ -1,5 +1,21 @@ 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 * events into one native "changed" event @@ -83,9 +99,16 @@ export class KeycloakWrapper extends EventTarget { if (this._silentCheckSsoUri) { options['onLoad'] = 'check-sso'; 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); + } } /** -- GitLab