Branchwell

Skillfarm / website · skillfarm.ai / public / js / biolock-page.js

Raw 2.9 KB on 443f4fb7f176dfa237ca639945f31e22fbe94e8c
1// Wires the BioLock buttons on the sign-in, settings and add-device pages.
2import { enrollThisDevice, enrolledOnThisDevice, hasBuiltInReader, signIn } from '/biolock.js';
3
4const post = async (url, body) => {
5 const r = await fetch(url, { method: 'POST', headers: { 'content-type': 'application/json', accept: 'application/json' }, body: JSON.stringify(body), credentials: 'same-origin' });
6 const data = await r.json().catch(() => ({}));
7 if (!r.ok) throw new Error(data.message || 'Something went wrong. Try again.');
8 return data;
9};
10
11const deviceName = () => {
12 const ua = navigator.userAgent;
13 if (/iPhone|Android.+Mobile/.test(ua)) return 'Phone';
14 if (/iPad|Android|Tablet/.test(ua) || (navigator.maxTouchPoints > 1 && /Macintosh/.test(ua))) return 'Tablet';
15 return 'Computer';
16};
17
18const friendly = (e) => {
19 if (e && (e.name === 'NotAllowedError' || e.name === 'AbortError')) return 'BioLock was cancelled or timed out. Try again.';
20 if (e && e.name === 'InvalidStateError') return 'This device already has BioLock for this account.';
21 return (e && e.message) || 'Something went wrong. Try again.';
22};
23
24for (const box of document.querySelectorAll('[data-biolock]')) {
25 const status = box.querySelector('[data-biolock-status]');
26 const say = (text) => { if (status) status.textContent = text; };
27 const next = box.dataset.next || '/';
28 const buttons = box.querySelectorAll('button');
29 const busy = (on) => buttons.forEach((b) => { b.disabled = on; });
30
31 hasBuiltInReader().then((ok) => {
32 if (!ok) say('This browser cannot reach a fingerprint or face reader on this device. BioLock needs one: try a phone, tablet or computer that has one, or turn it on in the device settings.');
33 });
34
35 box.querySelector('[data-biolock-signin]')?.addEventListener('click', async () => {
36 busy(true); say('Use your fingerprint or face.');
37 try {
38 const start = await post('/api/biolock/signin/options', { ids: enrolledOnThisDevice() });
39 const response = await signIn(start.options);
40 const done = await post('/api/biolock/signin/verify', { challengeId: start.challengeId, response, next });
41 location.assign(done.next || next);
42 } catch (e) { say(friendly(e)); busy(false); }
43 });
44
45 for (const btn of box.querySelectorAll('[data-biolock-register]')) {
46 btn.addEventListener('click', async () => {
47 const mode = btn.dataset.biolockRegister;
48 busy(true); say('Use your fingerprint or face.');
49 try {
50 const token = mode === 'link' ? location.hash.slice(1) : undefined;
51 const start = await post('/api/biolock/register/options', { mode, token });
52 const response = await enrollThisDevice(start.options);
53 const done = await post('/api/biolock/register/verify', { challengeId: start.challengeId, response, name: deviceName(), next });
54 location.assign(done.next || next);
55 } catch (e) { say(friendly(e)); busy(false); }
56 });
57 }
58}