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;
|
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) {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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;
|
|
|
|
console.log(data);
|
|
|
|
/*
|
|
|
|
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];
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.fetch(meta.baseUrl + meta.pair_request.pathname + '/' + magic, {
|
|
|
|
method: 'GET'
|
|
|
|
, cors: true
|
|
|
|
}).then(function (resp) {
|
|
|
|
return resp.json().then(function (data) {
|
|
|
|
console.log('Data:');
|
|
|
|
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;
|
|
|
|
document.querySelector('.js-magic-link').innerText = magic;
|
2018-06-21 11:19:10 +00:00
|
|
|
window.alert("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-06-21 06:13:05 +00:00
|
|
|
document.querySelector('.js-hostname').innerText = data.hostname || 'Device';
|
|
|
|
//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-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
|
|
|
}());
|