// #10: forced first-login password change. When an admin provisions a user // with must_change_password=1, route() in app.js redirects them here and blocks // every other view until they set a new password. Reuses the same PUT /api/auth/me // path as the Settings change-password form; on success the server clears // must_change_password, we refresh the cached user, and return to the app. import { api } from '../api.js'; import { t } from '../i18n.js'; import { showToast } from '../components/toast.js'; export async function render(container) { container.innerHTML = `

${t('forcepw.title')}

${t('forcepw.subtitle')}

${t('forcepw.hint')}

`; const current = container.querySelector('#fpwCurrent'); const next = container.querySelector('#fpwNew'); const confirm = container.querySelector('#fpwConfirm'); const submit = container.querySelector('#fpwSubmit'); const errorEl = container.querySelector('#fpwError'); current.focus(); const showError = (msg) => { errorEl.textContent = msg; errorEl.style.display = 'block'; }; async function doChange() { errorEl.style.display = 'none'; const cur = current.value; const nw = next.value; const cf = confirm.value; if (!cur || !nw) { showError(t('forcepw.error_required')); return; } if (nw.length < 8) { showError(t('forcepw.error_min8')); return; } if (nw !== cf) { showError(t('forcepw.error_mismatch')); return; } submit.disabled = true; submit.textContent = t('forcepw.submitting'); try { await api.updateMe({ password: nw, current_password: cur }); // Refresh the cached user so the (now-cleared) must_change_password flag // is reflected, then return to the app. try { const fresh = await api.getMe(); localStorage.setItem('user', JSON.stringify(fresh)); } catch { /* fall through; reload re-fetches */ } showToast(t('forcepw.success'), 'success'); window.location.hash = '#/'; window.location.reload(); } catch (err) { submit.disabled = false; submit.textContent = t('forcepw.submit'); showError(err?.message || t('forcepw.error_generic')); } } submit.addEventListener('click', doChange); [current, next, confirm].forEach(el => el.addEventListener('keydown', (e) => { if (e.key === 'Enter') doChange(); })); } export function cleanup() {}