// Anmeldung. Kein Berechtigungsschritt mehr: Der Serverzugriff ist im Manifest erlaubt. import { h, errText } from '../lib/dom.js'; import { login, normalizeServerUrl } from '../lib/core.js'; export async function renderLogin(app, ctx, prefill = {}) { const { lastLogin } = await chrome.storage.local.get('lastLogin'); const start = { serverUrl: prefill.serverUrl ?? lastLogin?.serverUrl ?? '', email: prefill.email ?? lastLogin?.email ?? '', needTotp: !!prefill.needTotp, error: prefill.error || '' }; let password = prefill.password || ''; const msg = h('div', { class: 'msg err', hidden: start.error ? undefined : true, role: 'alert' }, start.error); const server = h('input', { type: 'text', id: 'server', placeholder: 'support.example.de', value: start.serverUrl, autocomplete: 'url', required: true }); const email = h('input', { type: 'email', id: 'email', value: start.email, autocomplete: 'username', required: true }); const pass = h('input', { type: 'password', id: 'pass', autocomplete: 'current-password', required: !start.needTotp }); const code = h('input', { type: 'text', id: 'code', inputmode: 'numeric', pattern: '[0-9]{6}', maxlength: '6', autocomplete: 'one-time-code', placeholder: '6-stelliger Code' }); const button = h('button', { class: 'btn primary', type: 'submit' }, start.needTotp ? 'Bestätigen' : 'Anmelden'); const form = h('form', { onsubmit: async (ev) => { ev.preventDefault(); msg.hidden = true; button.disabled = true; try { if (!start.needTotp) password = pass.value; const r = await login(server.value, email.value.trim(), password, start.needTotp ? code.value.trim() : ''); if (r.requiresTotp) return renderLogin(app, ctx, { serverUrl: server.value, email: email.value, password, needTotp: true }); await chrome.storage.local.set({ lastLogin: { serverUrl: normalizeServerUrl(server.value), email: email.value.trim() } }); password = ''; await chrome.runtime.sendMessage({ type: 'session-changed' }).catch(() => {}); ctx.loggedIn(r.session); } catch (e) { msg.textContent = errText(e); msg.hidden = false; button.disabled = false; } }, }, start.needTotp ? [h('p', { class: 'muted' }, 'Dein Konto ist mit einem Authenticator geschützt. Bitte den aktuellen Code eingeben.'), h('label', {}, 'Authenticator-Code', code)] : [h('label', {}, 'Server-Adresse', server), h('label', {}, 'E-Mail-Adresse', email), h('label', {}, 'Passwort', pass)], msg, button); app.replaceChildren(h('div', { class: 'content' }, h('div', { class: 'login' }, h('div', { class: 'brand' }, h('img', { src: 'icons/icon128.png', alt: '' }), h('h1', {}, 'DeskHub'), h('div', { class: 'muted' }, 'Mit deinem Mitarbeiter-Konto anmelden')), form))); (start.needTotp ? code : (start.serverUrl ? pass : server)).focus(); }