telebit-relay.js/lib/extensions/admin/login/js/app.js

181 lines
5.8 KiB
JavaScript
Raw Normal View History

2018-06-15 08:45:47 +00:00
(function () {
'use strict';
2018-06-21 06:13:05 +00:00
var meta = {};
var magic;
2018-06-21 06:23:16 +00:00
var domainname;
var port;
2018-06-15 08:45:47 +00:00
2018-06-21 06:13:05 +00:00
function checkStatus() {
// TODO use Location or Link
window.fetch(meta.baseUrl + 'api/telebit.cloud/pair_state/' + magic, {
2018-06-15 08:45:47 +00:00
method: 'GET'
, cors: true
}).then(function (resp) {
2018-06-21 06:13:05 +00:00
return resp.json().then(function (data) {
console.log(data);
2018-06-21 11:02:53 +00:00
if ('invalid' === data.status) {
window.alert("something went wrong");
return;
}
if ('complete' === data.status) {
2018-09-13 23:44:38 +00:00
successScreen();
2018-06-21 11:02:53 +00:00
setTimeout(function () {
2018-06-29 12:07:25 +00:00
//window.document.body.innerHTML += ('<img src="https://' + domainname + '/_apis/telebit.cloud/clear.gif">');
2018-06-21 11:02:53 +00:00
// TODO once this is loaded (even error) Let's Encrypt is done,
// then it's time to redirect to the domain. Yay!
}, 1 * 1000);
return;
}
setTimeout(checkStatus, 2 * 1000);
2018-06-21 06:13:05 +00:00
}, function (err) {
console.error(err);
setTimeout(checkStatus, 2 * 1000);
});
});
}
2018-09-13 23:44:38 +00:00
function successScreen() {
document.querySelector('.js-authz').hidden = true;
document.querySelector('.js-finish-button').addEventListener('click', function(e) {
window.location.href='https://' + domainname + "/#/serviceport=" + port;
2018-09-13 23:44:38 +00:00
});
2018-09-14 05:06:04 +00:00
document.querySelectorAll('.js-new-domain').forEach(function(ele) {
ele.innerHTML = domainname;
});
2018-09-13 23:44:38 +00:00
document.querySelector('.js-finish').hidden = false;
}
2018-06-21 06:13:05 +00:00
function submitCode(pair) {
// TODO use Location or Link
document.querySelector('.js-magic').hidden = true;
window.fetch(meta.baseUrl + 'api/telebit.cloud/pair_code/', {
method: 'POST'
, headers: {
'Content-Type': 'application/json'
}
, body: JSON.stringify({
magic: pair.magic
, pin: pair.pin || pair.code
, agree_tos: pair.agreeTos
})
, cors: true
}).then(function (resp) {
return resp.json().then(function (data) {
2018-06-21 06:23:16 +00:00
// TODO check for error (i.e. bad Pair Code / PIN)
// shouldn't be pending (because we get here by being ready)
// should poll over 'ready'
2018-06-21 11:02:53 +00:00
2018-07-08 02:57:33 +00:00
console.log('Submit Code Response:');
console.log(data);
if (data.error) {
document.querySelector('.js-error').hidden = false;
return;
}
2018-06-21 06:23:16 +00:00
2018-06-21 06:13:05 +00:00
setTimeout(checkStatus, 0);
2018-06-21 06:23:16 +00:00
2018-06-21 06:13:05 +00:00
document.querySelector('.js-authz').hidden = false;
2018-09-13 23:44:38 +00:00
document.querySelector('.js-debug-container').hidden = false;
2018-07-08 02:57:33 +00:00
2018-06-21 06:13:05 +00:00
/*
document.querySelectorAll('.js-token-data').forEach(function ($el) {
$el.innerText = JSON.stringify(data, null, 2);
});
*/
document.querySelectorAll('.js-new-href').forEach(function ($el) {
2018-06-21 06:23:16 +00:00
domainname = data.domains[0];
port = data.port;
2018-06-21 06:13:05 +00:00
$el.href = 'https://' + data.domains[0] + '/';
$el.innerText = '🔐 https://' + data.domains[0];
});
document.querySelectorAll('.js-domainname').forEach(function ($el) {
$el.innerText = data.domains.join(',');
});
document.querySelectorAll('.js-serviceport').forEach(function ($el) {
$el.innerText = data.ports.join(',');
});
document.querySelectorAll('.js-token').forEach(function ($el) {
$el.innerText = data.jwt;
});
}, function (err) {
console.error(err);
document.querySelector('.js-error').hidden = false;
});
});
}
function init() {
magic = (window.location.hash || '').substr(2).replace(/magic=/, '');
if (!magic) {
document.querySelector('body').hidden = false;
document.querySelector('.js-error').hidden = false;
2018-07-08 02:57:33 +00:00
return;
2018-06-21 06:13:05 +00:00
}
window.fetch(meta.baseUrl + meta.pair_request.pathname + '/' + magic, {
method: 'GET'
, cors: true
}).then(function (resp) {
return resp.json().then(function (data) {
2018-07-08 03:02:09 +00:00
console.log('pair request data:');
2018-06-21 06:13:05 +00:00
console.log(data);
2018-06-21 11:19:10 +00:00
document.querySelector('body').hidden = false;
2018-06-21 06:13:05 +00:00
if (data.error) {
2018-06-15 09:00:16 +00:00
document.querySelector('.js-error').hidden = false;
2018-07-08 03:02:09 +00:00
document.querySelector('.js-magic-link').innerText = "Something went wrong. Perhaps an bad or expired link.";
2018-06-15 09:00:16 +00:00
return;
}
document.querySelector('.js-magic').hidden = false;
2018-09-13 23:44:38 +00:00
document.querySelectorAll('.js-hostname').forEach(function(ele) {
ele.innerText = data.hostname || 'Device';
});
2018-06-21 06:13:05 +00:00
//document.querySelector('.js-token-data').innerText = JSON.stringify(data, null, 2);
2018-06-15 08:45:47 +00:00
});
});
2018-06-21 06:13:05 +00:00
document.querySelector('.js-submit').addEventListener('submit', function (ev) {
ev.preventDefault();
var pair = {};
pair.magic = magic;
pair.code = document.querySelector('[name=pair-code]').value;
pair.agreeTos = document.querySelector('[name=letsencrypt-agree]').checked
&& document.querySelector('[name=telebit-agree]').checked;
console.log('Pair Form:');
console.log(pair);
submitCode(pair);
});
2018-09-13 07:39:33 +00:00
var formElements = document.querySelector('.js-submit').elements;
for(var i = 0; i < formElements.length; ++i) {
var tosCheck = document.querySelector('[name=telebit-agree]');
var leCheck = document.querySelector('[name=letsencrypt-agree]');
var pairCodeInput = document.querySelector('[name=pair-code]');
formElements[i].addEventListener('input', function(ev) {
if(tosCheck.checked && leCheck.checked && pairCodeInput.value.length) {
document.querySelector('.js-submit button').disabled = false;
} else {
document.querySelector('.js-submit button').disabled = true;
}
});
};
2018-09-13 23:44:38 +00:00
document.querySelector('.js-debug-button').addEventListener("click", function(e) {
document.querySelector('.js-debug-container').classList.toggle("visible");
})
2018-06-15 08:45:47 +00:00
}
2018-06-21 06:13:05 +00:00
window.fetch('https://' + location.hostname + '/_apis/telebit.cloud/index.json', {
method: 'GET'
, cors: true
}).then(function (resp) {
return resp.json().then(function (_json) {
meta = _json;
meta.baseUrl = 'https://' + meta.api_host.replace(/:hostname/g, location.hostname) + '/';
init();
});
});
2018-06-15 08:45:47 +00:00
}());