-
Notifications
You must be signed in to change notification settings - Fork 38
/
application.js
97 lines (84 loc) · 3.04 KB
/
application.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require rails-ujs
//= require material-components-web
//= require turbolinks
//= require_tree .
document.addEventListener("DOMContentLoaded", function(event) {
document.querySelectorAll(".mdc-text-field").forEach(function(textfield) {
new window.mdc.textfield.MDCTextField(textfield);
});
let menuElement = document.querySelector(".js-menu");
let menuOpenerElement = document.querySelector(".js-menu-opener");
if (menuElement && menuOpenerElement) {
let menu = new window.mdc.menu.MDCMenu(menuElement);
menuOpenerElement.addEventListener('click', function(event) {
menu.open = !menu.open;
});
menu.setAnchorCorner(window.mdc.menu.Corner.BOTTOM_START);
}
});
function binToStr(bin) {
return btoa(new Uint8Array(bin).reduce(
(s, byte) => s + String.fromCharCode(byte), ''
));
}
function strToBin(str) {
console.log(str);
return Uint8Array.from(atob(str), c => c.charCodeAt(0));
}
function callback(url, body) {
fetch(url, {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"X-CSRF-Token": document.querySelector('meta[name="csrf-token"]').getAttribute("content")
},
credentials: 'same-origin'
}).then(function() {
window.location.replace("/")
});
}
function create(callbackUrl, credentialOptions) {
navigator.credentials.create({ "publicKey": credentialOptions }).then(function(attestation) {
callback(callbackUrl, {
id: attestation.id,
response: {
clientDataJSON: binToStr(attestation.response.clientDataJSON),
attestationObject: binToStr(attestation.response.attestationObject)
}
});
}).catch(function(error) {
console.log(error);
});
console.log("Creating new public key credential...");
}
function get(credentialOptions) {
navigator.credentials.get({ "publicKey": credentialOptions }).then(function(credential) {
var assertionResponse = credential.response;
callback("/callback", {
id: binToStr(credential.rawId),
response: {
clientDataJSON: binToStr(assertionResponse.clientDataJSON),
signature: binToStr(assertionResponse.signature),
userHandle: binToStr(assertionResponse.userHandle),
authenticatorData: binToStr(assertionResponse.authenticatorData)
}
});
}).catch(function(error) {
console.log(error);
});
console.log("Getting public key credential...");
}