// Working demo. The server part runs in this page with the same server.js a real server uses,
// so you can try it with no account. Anything enrolled here works only for this demo.
import * as S from '../code/server.js';
import * as C from '../code/client.js';
const rpId = location.hostname, origin = location.origin;
const DB = 'biolock-demo';
const db = () => { try { return JSON.parse(localStorage.getItem(DB) || '{"creds":[]}'); } catch { return { creds: [] }; } };
const put = (v) => { try { localStorage.setItem(DB, JSON.stringify(v)); } catch { /* private mode */ } };
const $ = (s) => document.querySelector(s);
const say = (msg, ok = true) => { const el = $('#fp-status'); el.textContent = msg; el.dataset.ok = ok ? '1' : '0'; };
function render() {
const list = $('#fp-devices'); list.innerHTML = '';
const creds = db().creds;
if (!creds.length) { list.innerHTML = '
No devices yet. Enroll this one first.
'; return; }
for (const c of creds) {
const li = document.createElement('li');
const here = C.enrolledOnThisDevice(rpId).includes(c.id);
li.innerHTML = '';
li.querySelector('b').textContent = c.name + (here ? ' (this device)' : '');
li.querySelector('small').textContent = 'Enrolled ' + new Date(c.createdAt).toLocaleString() + (c.lastUsed ? ' ยท last used ' + new Date(c.lastUsed).toLocaleString() : '');
li.querySelector('button').onclick = async () => {
const d = db(); d.creds = d.creds.filter((x) => x.id !== c.id); put(d);
await C.removedOnServer(c.id, rpId); render(); say('Removed. That device can no longer sign in.');
};
list.appendChild(li);
}
}
$('#fp-add').onclick = async () => {
try {
const opts = S.registrationOptions({ rpId, rpName: 'BioLock demo', userName: 'demo', displayName: 'Demo member', excludeIds: C.enrolledOnThisDevice(rpId) });
const cred = await C.enrollThisDevice(opts);
const rec = await S.verifyRegistration({ credential: cred, expectedChallenge: opts.challenge, expectedOrigin: origin, rpId });
rec.name = ($('#fp-name').value || '').trim() || 'This device';
const d = db(); d.creds.push(rec); put(d); render();
say('Enrolled. This device now signs in with its own fingerprint or face.');
} catch (e) { say(nice(e), false); }
};
$('#fp-signin').onclick = async () => {
try {
const opts = S.signInOptions({ rpId, deviceCredentialIds: C.enrolledOnThisDevice(rpId) });
const cred = await C.signIn(opts);
const d = db(); const stored = d.creds.find((c) => c.id === cred.id);
if (!stored) { await C.removedOnServer(cred.id, rpId); throw new Error('This device was removed. Enroll it again.'); }
const r = await S.verifySignIn({ credential: cred, stored, expectedChallenge: opts.challenge, expectedOrigin: origin, rpId });
stored.counter = r.counter; stored.lastUsed = Date.now(); put(d); render();
say('Signed in with "' + stored.name + '". Checked: right site, fresh challenge, valid signature, fingerprint or face used.');
} catch (e) { say(nice(e), false); }
};
function nice(e) {
if (e && e.name === 'NotAllowedError') return 'Cancelled, or the reader did not match. Try again.';
if (e && e.name === 'InvalidStateError') return 'This device is already enrolled.';
return (e && e.message) ? e.message : String(e);
}
(async () => {
const builtIn = await C.hasBuiltInReader();
$('#fp-check').textContent = builtIn ? 'This device has a fingerprint or face reader. Ready.' : 'This device has no fingerprint or face reader, so it cannot sign in. Try a phone or a computer with a reader.';
if (!builtIn) { $('#fp-add').disabled = true; $('#fp-signin').disabled = true; }
render();
})();