-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
47 lines (42 loc) · 1.65 KB
/
client.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
// client.js
const server_url = 'https://4feb-183-100-197-18.ngrok-free.app'
document.getElementById('register').addEventListener('click', async () => {
const resp = await fetch(`${server_url}/generate-registration-options`);
const opts = await resp.json();
try {
const attResp = await SimpleWebAuthnBrowser.startRegistration(opts);
const verificationResp = await fetch(`${server_url}/verify-registration`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ attResp, opts }),
});
const verificationJSON = await verificationResp.json();
if (verificationJSON.verified) {
alert('Registration successful!');
} else {
alert('Registration failed!');
}
} catch (error) {
console.error(error);
}
});
document.getElementById('authenticate').addEventListener('click', async () => {
const resp = await fetch(`${server_url}/generate-authentication-options`);
const opts = await resp.json();
try {
const authResp = await SimpleWebAuthnBrowser.startAuthentication(opts);
const verificationResp = await fetch(`${server_url}/verify-authentication`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ authResp, opts }),
});
const verificationJSON = await verificationResp.json();
if (verificationJSON.verified) {
alert('Authentication successful!');
} else {
alert('Authentication failed!');
}
} catch (error) {
console.error(error);
}
});