Keep website password fields usable when email settings fail

This commit is contained in:
unimatrixgit 2026-11-29 07:45:24 +01:00
commit d3a27db1ca
3 changed files with 48 additions and 5 deletions

View file

@ -14,7 +14,7 @@ export function contactPayload(action,{email='',code='',confirmed=false}={}){
}
export function mountSecurity(host,{fetcher=fetch}={}){
let active=true,busy=false,uncertain=false;const controller=new AbortController(),{request}=createUserAPI(fetcher,controller.signal);
let active=true,busy=false,uncertain=false,authenticated=false,passwordBusy=false,passwordUncertain=false;const controller=new AbortController(),{request}=createUserAPI(fetcher,controller.signal);
const passwordBox=el('section',undefined,'cp-v2-personal-box'),contactBox=el('section',undefined,'cp-v2-personal-box'),body=el('div');body.append(passwordBox,contactBox);
const passwordForm=el('form'),passwordFields=el('fieldset');passwordFields.disabled=true;
passwordBox.append(el('h4','Website password'),el('p','Changing your password signs out your other website sessions. Your mailbox password remains separate.'));
@ -36,9 +36,9 @@ export function mountSecurity(host,{fetcher=fetch}={}){
}
void loadPaymentSecurity();
const {root}=createCPV2Page({shellHost:host,title:'Account Security',description:'Protect your website account and manage verified support notifications.',artwork:'nav-account-v2.webp',tabs:[{id:'password',label:'Website password',content:passwordBox},{id:'notifications',label:'Notifications',content:contactBox},{id:'payment-security',label:'Payments & ATM',content:bankBox}]});host.replaceChildren(root);
function controls(){passwordFields.disabled=busy||uncertain;reload.disabled=busy;for(const control of mailBody.querySelectorAll('button,input'))control.disabled=busy||uncertain;}
async function load(){if(busy)return;busy=true;passwordFields.disabled=true;reload.disabled=true;mailBody.replaceChildren();
try{const session=await request('/api/auth/session');if(!session.authenticated)throw Error('Sign in to manage account security.');const data=await request('/api/account/email');if(!active)return;uncertain=false;
function controls(){passwordFields.disabled=!authenticated||passwordBusy||passwordUncertain;reload.disabled=busy;for(const control of mailBody.querySelectorAll('button,input'))control.disabled=busy||uncertain;}
async function load(){if(busy)return;busy=true;reload.disabled=true;mailBody.replaceChildren();
try{const session=await request('/api/auth/session');authenticated=!!session.authenticated;controls();if(!authenticated)throw Error('Sign in to manage account security.');passwordUncertain=false;controls();const data=await request('/api/account/email');if(!active)return;uncertain=false;
mailStatus.textContent=data.verified?'Verified contact: '+data.email:data.pendingEmail?'Verification pending: '+data.pendingEmail:'No verified contact email. Read updates on the Support website.';
const form=el('form'),fields=el('fieldset'),email=input(fields,'Email address','email');email.value=data.email||data.pendingEmail||'';email.autocomplete='email';email.maxLength=254;email.required=true;const consent=input(fields,'I confirm this change to my notification email.','checkbox');consent.required=true;
const send=el('button','Send verification email'),remove=el('button','Remove contact email');send.type=remove.type='submit';remove.formNoValidate=true;remove.hidden=!data.email&&!data.pendingEmail;fields.append(send,remove);form.append(fields);mailBody.append(form);
@ -49,6 +49,6 @@ export function mountSecurity(host,{fetcher=fetch}={}){
finally{busy=false;if(active)controls();}
}
async function act(action,fields={}){if(busy||uncertain)return;let payload;try{payload=contactPayload(action,fields);}catch(error){mailStatus.textContent=error.message;return;}busy=true;controls();mailStatus.textContent='Updating notification settings…';try{const result=await request('/api/account/email',payload);if(!active)return;busy=false;await load();if(active)mailStatus.textContent=result.message||'Notification settings updated.';}catch(error){if(active){uncertain=!!error.uncertain;mailStatus.textContent=error.message+(uncertain?' Reload settings before retrying.':'');}}finally{busy=false;if(active)controls();}}
passwordForm.onsubmit=async event=>{event.preventDefault();if(busy||uncertain)return;let payload;try{payload=passwordPayload(current.value,fresh.value,passwordConsent.checked);}catch(error){passwordStatus.textContent=error.message;return;}busy=true;controls();passwordStatus.textContent='Changing your website password…';try{const result=await request('/api/auth/password',payload);if(result.ok!==true)throw Object.assign(Error('The password change could not be confirmed.'),{uncertain:true});if(active){passwordForm.reset();passwordStatus.textContent='Password changed. Other website sessions have been signed out.';}}catch(error){if(active){uncertain=!!error.uncertain;passwordStatus.textContent=error.message+(uncertain?' Reload settings and check your login before retrying.':'');}}finally{current.value=fresh.value='';busy=false;if(active)controls();}};
passwordForm.onsubmit=async event=>{event.preventDefault();if(!authenticated||passwordBusy||passwordUncertain)return;let payload;try{payload=passwordPayload(current.value,fresh.value,passwordConsent.checked);}catch(error){passwordStatus.textContent=error.message;return;}passwordBusy=true;controls();passwordStatus.textContent='Changing your website password…';try{const result=await request('/api/auth/password',payload);if(result.ok!==true)throw Object.assign(Error('The password change could not be confirmed.'),{uncertain:true});if(active){passwordForm.reset();passwordStatus.textContent='Password changed. Other website sessions have been signed out.';}}catch(error){if(active){passwordUncertain=!!error.uncertain;passwordStatus.textContent=error.message+(passwordUncertain?' Reload settings and check your login before retrying.':'');}}finally{current.value=fresh.value='';passwordBusy=false;if(active)controls();}};
reload.onclick=()=>void load();void load();return()=>{active=false;controller.abort();current.value=fresh.value='';root.remove();};
}

View file

@ -0,0 +1,7 @@
# Security form correction — 29 November 2026
Owner could not type into the Current website password field. Source diagnosis: the password fieldset inherited busy/uncertain state from notification-email loading; an unrelated email failure disabled the whole password form with its error hidden on another tab.
Password fields now have their own operation state and are enabled as soon as the existing authenticated session is verified. Email requests, failures and uncertain email writes cannot disable password editing/submission. Signed-out visitors remain disabled; uncertain password writes still require reload rather than an automatic retry. No accounts, roles or passwords are changed by this frontend correction.
Validation: 15 focused tests passed, including stalled and failed email requests followed by successful password submission, signed-out gating, prior API password-policy tests and actual form-mode switching. Runtime publication must use the canonical web workflow.

View file

@ -0,0 +1,36 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import vm from 'node:vm';
import {createUserAPI} from '../control-panel/public/cp-v2/cp-v2-user-api.mjs';
function mount(fetcher){
const nodes=[];
function el(tag,text){const n={tag,textContent:text||'',children:[],disabled:false,value:'',append(...v){this.children.push(...v);},replaceChildren(...v){this.children=v;},setAttribute(){},querySelectorAll(){return [];},reset(){},remove(){}};nodes.push(n);return n;}
const source=readFileSync(new URL('../control-panel/public/cp-v2/cp-v2-security.mjs',import.meta.url),'utf8').replace(/^import .*;\r?\n/gm,'').replaceAll('export ','');
const context=vm.createContext({el,createUserAPI,AbortController,createCPV2Page(){return {root:el('main')};}});
vm.runInContext(source,context);context.mountSecurity(el('div'),{fetcher});
return nodes;
}
const flush=()=>new Promise(resolve=>setImmediate(resolve));
test('password fields work after authenticated session even while notification request stalls or fails',async()=>{
let failEmail;const writes=[];
const nodes=mount(async(path,init)=>{
if(path==='/api/auth/session')return Response.json({authenticated:true});
if(path==='/api/account/email')return new Promise(resolve=>{failEmail=()=>resolve(Response.json({message:'Email service unavailable'},{status:503}));});
if(path==='/api/auth/password'){writes.push(JSON.parse(init.body));return Response.json({ok:true});}
return Response.json({message:'Unavailable'},{status:503});
});
await flush();const fields=nodes.find(n=>n.tag==='fieldset'),form=nodes.find(n=>n.tag==='form');
assert.equal(fields.disabled,false,'email loading must not lock website password');
failEmail();await flush();assert.equal(fields.disabled,false,'email failure must not lock website password');
const [current,fresh,consent]=nodes.filter(n=>n.tag==='input');current.value='temporary';fresh.value='chosen';consent.checked=true;
await form.onsubmit({preventDefault(){}});
assert.deepEqual(writes,[{currentPassword:'temporary',newPassword:'chosen'}]);assert.equal(fields.disabled,false);
});
test('password form remains disabled for signed-out visitors',async()=>{
const nodes=mount(async path=>Response.json(path==='/api/auth/session'?{authenticated:false}:{message:'Unavailable'},{status:path==='/api/auth/session'?200:503}));
await flush();assert.equal(nodes.find(n=>n.tag==='fieldset').disabled,true);
});