'use strict'; /* Login / signup. Not in the nav. The invite field appears when the server answers signup_closed. */ (function () { const { h, icon, clear } = UI; let box, mode = 'login', needInvite = false, busy = false; let refs = {}; function deviceName() { const ua = navigator.userAgent || ''; const browser = /Edg\//.test(ua) ? 'Edge' : /OPR\//.test(ua) ? 'Opera' : /Firefox\//.test(ua) ? 'Firefox' : /Chrome\//.test(ua) ? 'Chrome' : /Safari\//.test(ua) ? 'Safari' : 'Browser'; const os = /Android/.test(ua) ? 'Android' : /iPhone|iPad|iPod/.test(ua) ? 'iOS' : /Windows/.test(ua) ? 'Windows' : /Mac OS/.test(ua) ? 'macOS' : /Linux/.test(ua) ? 'Linux' : ''; return (browser + (os ? ' on ' + os : '')).slice(0, 40); } function showError(msg) { refs.err.textContent = msg || ''; refs.err.hidden = !msg; } function validationMessage(err) { const list = err.details && err.details.errors; const base = App.t('err.validation_failed'); if (Array.isArray(list) && list.length) { const f = list[0]; const path = String(f.path || '').replace(/^\$?\.?/, ''); return base + (path ? ' (' + path + ')' : ''); } return base; } async function submit(ev) { ev.preventDefault(); if (busy) return; showError(''); const username = refs.user.value.trim(), password = refs.pass.value; if (!username || !password) { showError(App.t('err.validation_failed')); return; } busy = true; refs.go.disabled = true; refs.goText.textContent = App.t('auth.working'); try { let r; if (mode === 'signup') { const body = { username, password, device: deviceName() }; if (needInvite && refs.invite.value.trim()) body.invite = refs.invite.value.trim(); r = await App.api.post('/auth/signup', body, { auth: false }); } else { r = await App.api.post('/auth/login', { username, password, device: deviceName() }, { auth: false }); } App.api.setToken(r.token); App.setSession(r.user, r.operators); refs.pass.value = ''; App.authNotice = null; await App.enter('pop'); } catch (e) { if (e.code === 'signup_closed') { needInvite = true; draw(); refs.invite.focus(); showError(App.t('auth.invite.hint')); } else if (e.code === 'validation_failed') showError(validationMessage(e)); else showError(App.errMsg(e)); } finally { busy = false; if (refs.go) { refs.go.disabled = false; refs.goText.textContent = App.t(mode === 'signup' ? 'auth.signup' : 'auth.login'); } } } function field(id, label, input, hint) { return h('label', { class: 'field', for: id }, h('span', { class: 'field-lbl', text: label }), input, hint ? h('span', { class: 'field-hint', text: hint }) : null); } function draw() { const keep = { u: refs.user ? refs.user.value : '', p: refs.pass ? refs.pass.value : '', i: refs.invite ? refs.invite.value : '', e: refs.err ? refs.err.textContent : '' }; clear(box); const signup = mode === 'signup'; refs.user = h('input', { id: 'f-user', name: 'username', type: 'text', autocomplete: 'username', autocapitalize: 'none', autocorrect: 'off', spellcheck: 'false', required: true, minlength: signup ? '3' : null, maxlength: '20', pattern: signup ? '[A-Za-z0-9_\\-]{3,20}' : null, enterkeyhint: 'next' }); refs.pass = h('input', { id: 'f-pass', name: 'password', type: 'password', autocomplete: signup ? 'new-password' : 'current-password', required: true, minlength: signup ? '8' : null, maxlength: '128', enterkeyhint: needInvite && signup ? 'next' : 'go' }); refs.invite = h('input', { id: 'f-invite', name: 'invite', type: 'text', autocomplete: 'off', autocapitalize: 'none', spellcheck: 'false', maxlength: '64', enterkeyhint: 'go' }); refs.user.value = keep.u; refs.pass.value = keep.p; refs.invite.value = keep.i; refs.err = h('p', { class: 'form-error', role: 'alert', hidden: true }); refs.goText = h('span', { text: App.t(signup ? 'auth.signup' : 'auth.login') }); refs.go = h('button', { class: 'btn primary wide', type: 'submit' }, icon('check'), refs.goText); const notice = App.authNotice ? h('p', { class: 'notice', text: App.authNotice }) : null; const form = h('form', { class: 'auth-form', novalidate: false, onsubmit: submit }, h('h1', { class: 'auth-h', text: App.t(signup ? 'auth.signup' : 'auth.login') }), notice, field('f-user', App.t('auth.username'), refs.user, signup ? App.t('auth.username.hint') : null), field('f-pass', App.t('auth.password'), refs.pass, signup ? App.t('auth.password.hint') : null), signup && needInvite ? field('f-invite', App.t('auth.invite'), refs.invite) : null, refs.err, refs.go, h('button', { class: 'linklike', type: 'button', onclick: () => { mode = signup ? 'login' : 'signup'; draw(); } }, App.t(signup ? 'auth.toLogin' : 'auth.toSignup'))); box.appendChild(h('div', { class: 'auth-card' }, h('div', { class: 'auth-logo' }, h('span', { class: 'brand-mark big', 'aria-hidden': 'true' }), h('span', { class: 'auth-name', text: App.t('app.name') })), h('p', { class: 'auth-tag', text: App.t('app.tagline') }), form, h('div', { class: 'lang-switch', role: 'group', 'aria-label': App.t('settings.language') }, ...['en', 'nb'].map((l) => h('button', { class: 'chip-btn', type: 'button', 'aria-pressed': String(App.state.lang === l), onclick: () => App.setLang(l) }, l === 'en' ? 'EN' : 'NB'))))); if (keep.e) showError(keep.e); } App.registerScreen({ id: 'auth', titleKey: 'app.name', nav: false, hud: false, render(container) { box = container; }, onShow() { draw(); if (window.matchMedia('(pointer: fine)').matches) setTimeout(() => { if (refs.user) refs.user.focus({ preventScroll: true }); }, 50); }, onLang() { draw(); }, }); }());