feat: a new account e-mail is confirmed with a code, and the old one is told
Changing the e-mail in Dados da conta took effect at once, with nothing checking the new address: a typo locked the customer out, and anyone at a signed-in computer who knew the password could move the account to their own e-mail and then reset its password. Now the code goes to the new address, the account changes only once it is entered (same screen and limits as signup, and only the account that asked can complete it), and the old address gets "O e-mail da sua conta foi trocado", with the WhatsApp to call if it was not them. Without e-mail on, it changes at once as before. Signup and e-mail change share one code check (expiry, five attempts, the 60 s resend wait). Tested: mail_test (wrong password, same and taken e-mail, wrong code, another account's attempt, resend wait, the notice), the full suite in a CI-style stack, and the screen in Brave. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -310,12 +310,51 @@ $('fPerfil').onsubmit=async event=>{
|
||||
conta=r.customer;aviso('Dados salvos.');
|
||||
}catch(e){aviso(e.status===422?(algum?'Confira o WhatsApp e o endereço: todos os campos, menos o complemento, são necessários.':'Confira o WhatsApp.'):e.message,true);}
|
||||
};
|
||||
// A new e-mail is confirmed with the code sent to it before the account changes.
|
||||
let troca=null, trocaTimer=null;
|
||||
function emailTrocado(r){
|
||||
conta=r.customer;$('quem').textContent=conta.mail;$('dMail').value=conta.mail;
|
||||
fechaTroca();aviso('E-mail alterado. Use o novo e-mail para entrar.');
|
||||
}
|
||||
function fechaTroca(){
|
||||
clearInterval(trocaTimer);troca=null;$('fEmailCodigo').hidden=true;$('fEmail').hidden=false;
|
||||
}
|
||||
function trocaContagem(segundos){
|
||||
clearInterval(trocaTimer);
|
||||
const b=$('emailReenviar');let falta=segundos;
|
||||
const pinta=()=>{b.disabled=falta>0;b.textContent=falta>0?'Reenviar código ('+falta+' s)':'Reenviar código';};
|
||||
pinta();trocaTimer=setInterval(()=>{falta--;pinta();if(falta<=0)clearInterval(trocaTimer);},1000);
|
||||
}
|
||||
$('fEmail').onsubmit=async event=>{
|
||||
event.preventDefault();
|
||||
try{const r=await api('/account/email',{email:$('dMail').value,password:$('dMailSenha').value});
|
||||
conta=r.customer;$('dMailSenha').value='';$('quem').textContent=conta.mail;aviso('E-mail alterado. Use o novo e-mail para entrar.');}
|
||||
catch(e){aviso(e.status===422?'Confira o e-mail.':e.message,true);}
|
||||
event.preventDefault();$('dMailOk').disabled=true;
|
||||
try{
|
||||
const r=await api('/account/email',{email:$('dMail').value,password:$('dMailSenha').value});
|
||||
$('dMailSenha').value='';
|
||||
if(!r.verify){emailTrocado(r);return;}
|
||||
troca=r.verify;$('emailNovo').textContent=r.email;$('emailCodigo').value='';
|
||||
$('fEmail').hidden=true;$('fEmailCodigo').hidden=false;aviso('');trocaContagem(r.resend_seconds);$('emailCodigo').focus();
|
||||
}catch(e){aviso(e.status===422&&!e.message.startsWith('Este')?'Confira o e-mail.':e.message,true);}
|
||||
finally{$('dMailOk').disabled=false;}
|
||||
};
|
||||
$('emailCodigo').addEventListener('input',e=>{
|
||||
e.target.value=e.target.value.replace(/\D/g,'').slice(0,6);
|
||||
if(e.target.value.length===6&&!$('emailCodigoOk').disabled)$('fEmailCodigo').requestSubmit();
|
||||
});
|
||||
$('fEmailCodigo').onsubmit=async event=>{
|
||||
event.preventDefault();$('emailCodigoOk').disabled=true;
|
||||
try{emailTrocado(await api('/account/email/verify',{change:troca,code:$('emailCodigo').value}));}
|
||||
catch(e){
|
||||
aviso(e.status===422?'O código tem 6 números.':e.message,true);$('emailCodigo').select();
|
||||
if(e.status===410||e.status===429){$('emailReenviar').disabled=false;$('emailReenviar').textContent='Reenviar código';clearInterval(trocaTimer);}
|
||||
}finally{$('emailCodigoOk').disabled=false;}
|
||||
};
|
||||
$('emailReenviar').onclick=async()=>{
|
||||
$('emailReenviar').disabled=true;
|
||||
try{const r=await api('/account/email/resend',{change:troca});aviso('Enviamos um código novo para '+$('emailNovo').textContent+'.');
|
||||
trocaContagem(r.resend_seconds);$('emailCodigo').value='';$('emailCodigo').focus();}
|
||||
catch(e){aviso(e.message,true);if(e.status===410)fechaTroca();else $('emailReenviar').disabled=false;}
|
||||
};
|
||||
$('emailCancelar').onclick=()=>{aviso('');$('dMail').value=conta.mail;fechaTroca();};
|
||||
$('fSenha').onsubmit=async event=>{
|
||||
event.preventDefault();
|
||||
try{await api('/account/password',{current:$('dAtual').value,new:$('dNova').value});
|
||||
|
||||
Reference in New Issue
Block a user