-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.js
81 lines (72 loc) · 2.14 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"use strict";
document.addEventListener("DOMContentLoaded", function() {
let elem = document.getElementById('authn_test');
let key = JSON.parse(elem.attributes['data-key'].value);
var authnId = null;
var status = {};
let updateView = function () {
elem.innerHTML = JSON.stringify(status, null, 2);
}
setInterval(function() {
if (!authnId) return;
let handleError = (err) => {
status.error = 'authn get error (fetch)';
updateView();
console.log(err);
};
fetch(status.authn.url, {credentials: 'include'}).then((resp) => {
console.log(resp);
if (!resp.ok) {
status.error = 'authn get error (!ok)';
updateView();
return;
}
return resp.json().then((json) => {
console.log(json);
if (json.authn) {
status.authn = json.authn;
if (status.authn.status == 'verified') {
authnId = null;
}
} else {
status.error = 'authn get error';
}
updateView();
});
}).catch(handleError);
}, 1000);
document.getElementById("open_authn_button").addEventListener("click", function() {
window.open(status.authn.html_url, '_blank');
});
document.getElementById("start_authn_button").addEventListener("click", function() {
let payload = JSON.stringify({
name: "testuser",
comment: "test authn",
keys: [key],
});
let handleError = (err) => {
status.error = 'authn create error (fetch)';
updateView();
console.log(err);
};
fetch(`/api/authn`, {credentials: 'include', method: 'POST', body: payload}).then((resp) => {
console.log(resp);
if (!resp.ok) {
status.error = 'authn create error (!ok)';
updateView();
return;
}
return resp.json().then((json) => {
console.log(json);
if (json.authn) {
status.authn = json.authn;
authnId = json.authn.id;
document.getElementById("open_authn_button").className = '';
} else {
status.error = 'authn create error';
}
updateView();
});
}).catch(handleError);
});
});