feat: a new account is confirmed with a code sent to its e-mail
All checks were successful
Build and deploy / Validate source (push) Successful in 17s
Build and deploy / Integration suite on a real stack (push) Successful in 4m25s
Build and deploy / Secret scan and release gate (push) Successful in 9s
Build and deploy / Publish images (push) Successful in 1m22s

Anyone could open an account on someone else's e-mail. Now "Criar conta"
e-mails a 6-digit code, and the account exists only once it is entered: one
big field that takes a paste or the phone's autofill and confirms by itself,
"Reenviar código" after 60 s, "Trocar e-mail", and the sign-in box hidden
meanwhile.

Security: 15 minutes per code, 5 wrong attempts, at most 5 codes, limits per
address and per source; the password is hashed at the first step and the code
stored only as a hash, its e-mail erased once sent. An address that already
has an account gets the same answer on screen and a "você já tem conta"
e-mail instead of a code, so the form does not reveal who is registered.
Without e-mail (MAIL_ADAPTER=off) the account is created at once, as before.

The browser test reads the code from a local-only endpoint that exists only
where e-mail stays in the log. Tested: mail_test (wrong, locked, resent,
expired codes, existing address, erasure after SMTP), the full suite and the
browser test in a CI-style stack, and the screens in Brave on desktop and phone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-14 15:18:37 -03:00
parent 5a431f8422
commit 95be624cca
14 changed files with 337 additions and 29 deletions

View File

@@ -333,7 +333,10 @@ async function migrateGuestCart(previous,next){
}
async function authenticate(path,body,destino){
const previous=scope,guest=!conta;
const r=await api(path,body);forgetCheckout();
const r=await api(path,body);
// With e-mail on, a new account waits for the code sent to the address.
if(r.verify){pedeCodigo(r);return;}
forgetCheckout();
const next=(await api('/session')).cart_scope;scope=next;
if(guest)try{await migrateGuestCart(previous,next);}catch{aviso('Não foi possível transferir o carrinho salvo neste navegador.',true);}
conta=r.customer;$('quem').textContent=conta.mail;
@@ -342,7 +345,55 @@ async function authenticate(path,body,destino){
await mostra();
}
$('login').onsubmit=async event=>{event.preventDefault();try{await authenticate('/account/login',{email:$('email').value,password:$('password').value});}catch(e){aviso(e.message,true);}};
$('register').onsubmit=async event=>{event.preventDefault();try{await authenticate('/account/register',{customer:{cnpj:$('cnpj').value,zap:$('phone').value,mail:$('register-email').value},password:$('register-password').value});}catch(e){aviso(e.status===422?'Confira o CNPJ, o WhatsApp e o e-mail. A senha precisa ter 12 caracteres ou mais.':e.message,true);}};
$('register').onsubmit=async event=>{event.preventDefault();$('criarConta').disabled=true;
try{await authenticate('/account/register',{customer:{cnpj:$('cnpj').value,zap:$('phone').value,mail:$('register-email').value},password:$('register-password').value});}
catch(e){aviso(e.status===422?'Confira o CNPJ, o WhatsApp e o e-mail. A senha precisa ter 12 caracteres ou mais.':e.message,true);}
finally{$('criarConta').disabled=false;}};
// Signup code: the account is created only once the code e-mailed to the
// address is entered. Six digits confirm by themselves.
let signup=null, reenvioTimer=null;
function pedeCodigo(r){
signup=r.verify;
$('codigoMail').textContent=r.email;$('codigoMin').textContent=r.minutes;
// Only the code on screen while it is awaited: "Entrar" comes back with "Trocar e-mail".
$('register').hidden=true;$('login').hidden=true;$('fEsqueci').hidden=true;$('fCodigo').hidden=false;$('codigo').value='';
aviso('');contagem(r.resend_seconds);$('codigo').focus();
}
function contagem(segundos){
clearInterval(reenvioTimer);
const b=$('reenviar');let falta=segundos;
const pinta=()=>{b.disabled=falta>0;b.textContent=falta>0?'Reenviar código ('+falta+' s)':'Reenviar código';};
pinta();reenvioTimer=setInterval(()=>{falta--;pinta();if(falta<=0)clearInterval(reenvioTimer);},1000);
}
function voltaCadastro(){
clearInterval(reenvioTimer);signup=null;
$('fCodigo').hidden=true;$('register').hidden=false;$('login').hidden=false;$('register-email').focus();
}
$('codigo').addEventListener('input',e=>{
e.target.value=e.target.value.replace(/\D/g,'').slice(0,6);
if(e.target.value.length===6&&!$('codigoOk').disabled)$('fCodigo').requestSubmit();
});
$('fCodigo').onsubmit=async event=>{
event.preventDefault();$('codigoOk').disabled=true;
try{
await authenticate('/account/register/verify',{signup,code:$('codigo').value},'/conta');
clearInterval(reenvioTimer);signup=null;$('fCodigo').hidden=true;$('register').hidden=false;$('login').hidden=false;
$('register-password').value='';aviso('Conta criada. Bem-vindo!');
}catch(e){
aviso(e.status===422?'O código tem 6 números.':e.message,true);
$('codigo').select();
// Expired or used: the field stays, and "Reenviar código" is the way on.
if(e.status===410||e.status===429){$('reenviar').disabled=false;$('reenviar').textContent='Reenviar código';clearInterval(reenvioTimer);}
}finally{$('codigoOk').disabled=false;}
};
$('reenviar').onclick=async()=>{
$('reenviar').disabled=true;
try{const r=await api('/account/register/resend',{signup});aviso('Enviamos um código novo para '+$('codigoMail').textContent+'.');
contagem(r.resend_seconds);$('codigo').value='';$('codigo').focus();}
catch(e){aviso(e.message,true);if(e.status===410)voltaCadastro();else $('reenviar').disabled=false;}
};
$('trocarMail').onclick=()=>{aviso('');voltaCadastro();};
// Password recovery: a link by e-mail when the server sends e-mail,
// otherwise the WhatsApp. The link arrives in the address's fragment.
let recovery=false, resetToken='';