Skip to content

Instantly share code, notes, and snippets.

@ShankarSumanth
Created February 22, 2017 02:17
Show Gist options
  • Select an option

  • Save ShankarSumanth/2a137308c8e3b88c66ee2ede18df8edb to your computer and use it in GitHub Desktop.

Select an option

Save ShankarSumanth/2a137308c8e3b88c66ee2ede18df8edb to your computer and use it in GitHub Desktop.
function setupCheckLoginIframe() {
var promise = createPromise();
if (!loginIframe.enable) {
promise.setSuccess();
return promise.promise;
}
if (loginIframe.iframe) {
promise.setSuccess();
return promise.promise;
}
var iframe = document.createElement('iframe');
loginIframe.iframe = iframe;
iframe.onload = function() {
var realmUrl = getRealmUrl();
if (realmUrl.charAt(0) === '/') {
loginIframe.iframeOrigin = getOrigin();
} else {
loginIframe.iframeOrigin = realmUrl.substring(0, realmUrl.indexOf('/', 8));
}
promise.setSuccess();
// Remove the check as this check is implicit
//setTimeout(check, loginIframe.interval * 1000);
}
var src = getRealmUrl() + '/protocol/openid-connect/login-status-iframe.html';
iframe.setAttribute('src', src );
iframe.style.display = 'none';
document.body.appendChild(iframe);
var messageCallback = function (event) {
if (event.origin !== loginIframe.iframeOrigin) {
return;
}
var data = JSON.parse(event.data);
var promise = loginIframe.callbackMap[data.callbackId];
delete loginIframe.callbackMap[data.callbackId];
if ((!kc.sessionId || kc.sessionId == data.session) && data.loggedIn) {
promise.setSuccess(data.loggedIn);// add the data.loggedIn to the promise Success Method
} else {
kc.clearToken();
promise.setError();
}
};
window.addEventListener('message', messageCallback, false);
// move this check outside of this function as we will use the success of the promise to
// setup the check interval.
/**
var check = function() {
checkLoginIframe();
if (kc.token) {
setTimeout(check, loginIframe.interval * 1000);
}
};*/
return promise.promise;
}
function checkLogin() {
checkLoginIframe().success(function () {
if (kc.token) {
setTimeout(checkLogin, loginIframe.interval * 1000);
}
}).error(function () {
window.location.href = window.location.protocol + "//" + window.location.host;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment