09 plugin.js (5/5)
$('[data-act=save]').onclick = async () => {
const next = {};
for (const el of inputs) next[el.dataset.k] = el.value.trim();
next.server = next.server.replace(/\/+$/, '');
if (!/^https?:\/\//.test(next.server)) return out('Server must start with http:// or https://', false);
out('Signing in...');
try {
const req = loginRequest(next);
const r = await api.relay(req.method, req.url, req.headers, req.body);
let json = null;
try { json = JSON.parse(r.body); } catch (e) { /* not JSON */ }
const token = tokenFrom(json);
if (r.status < 200 || r.status >= 300 || !token) {
throw new Error((json && (json.message || json.error)) || 'HTTP ' + r.status);
}
await write('config.json', next);
await write('token.json', { token });
out('Signed in and saved. Open Home -> Plugins on the reader.');
} catch (e) {
out('Sign-in failed: ' + e.message, false);
}
};
$('[data-act=signout]').onclick = async () => {
// An empty file is rejected by writeFile; {} means "no token".
await write('token.json', {});
await write('config.json', { server: $('[data-k=server]').value.trim() });
$('[data-k=password]').value = '';
out('Signed out. Credentials removed from the SD card.');
};
});