06 plugin.js (2/2)
for (const el of container.querySelectorAll('[data-k]')) el.value = cfg[el.dataset.k] || '';
$('[data-act=save]').onclick = async () => {
const next = {};
for (const el of container.querySelectorAll('[data-k]')) next[el.dataset.k] = el.value.trim();
next.server = next.server.replace(/\/+$/, '');
out('Signing in...');
try {
// Same request the device will make, sent through the reader.
const r = await api.relay('POST', next.server + '/oauth/token',
{ 'Content-Type': 'application/json' },
JSON.stringify({ username: next.username, password: next.password }));
const body = JSON.parse(r.body || '{}');
if (r.status !== 200 || !body.access_token) throw new Error(body.message || ('HTTP ' + r.status));
await write('config.json', next);
await write('token.json', { token: body.access_token }); // the device can start right away
out('Signed in and saved. Open Home -> Plugins -> Mock Library on the reader.');
} catch (e) {
out('Sign-in failed: ' + e.message, false);
}
};
$('[data-act=signout]').onclick = async () => {
await write('token.json', {}); // empty file is rejected, {} is "no token"
await write('config.json', { server: cfg.server || '' }); // forget the credentials
out('Signed out.');
};
});