fix(ui): keep recovery codes on screen and report failed logout

While one-time codes are visible the 401 redirect is suppressed and a
beforeunload guard is active until the done button. MFA buttons follow the
account state. A failed logout shows an error instead of looking successful.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas RayanandClaude Opus 5.5 committed 2026-11-04 21:03:27 -03:00
1 parent 348e61ca95
commit 513741e403
5 files changed
+60 -8

No files matched your search

+40 -4
View File
@@ -155,6 +155,34 @@ async function changePassword(event) {
}, tr('Senha trocada. As outras sessões foram encerradas.'));
}
/** True while one-time recovery codes are on screen and not yet confirmed. */
let holdingCodes = false;
export function holdingOneTimeCodes() {
return holdingCodes;
}
function warnBeforeLeaving(event) {
event.preventDefault();
event.returnValue = '';
}
function holdCodes() {
holdingCodes = true;
window.addEventListener('beforeunload', warnBeforeLeaving);
}
function releaseCodes() {
holdingCodes = false;
window.removeEventListener('beforeunload', warnBeforeLeaving);
}
/** The buttons follow the account: disabling needs an analyst with MFA on. */
function showMfaState() {
$('#mfaStart').textContent = session.mfa_enabled ? 'Cadastrar MFA de novo' : 'Ativar MFA';
$('#mfaDisable').hidden = session.role === 'admin' || !session.mfa_enabled;
}
function bindMfa() {
const status = $('#mfaStatus');
$('#mfaStart').addEventListener('click', () => attempt(status, async () => {
@@ -172,6 +200,8 @@ function bindMfa() {
method: 'POST',
body: { code: $('#mfaCode').value.trim() },
}));
// This session has just ended, and the page must not leave with the codes.
holdCodes();
$('#mfaEnroll').hidden = true;
$('#mfaQr').removeAttribute('src');
$('#mfaKey').value = '';
@@ -188,11 +218,17 @@ function bindMfa() {
status,
});
// Activation ended every session of this user, this one included.
$('#mfaRecoveryDone').addEventListener('click', () => location.replace('/login.html'));
$('#mfaRecoveryDone').addEventListener('click', () => {
releaseCodes();
location.replace('/login.html');
});
$('#mfaDisable').addEventListener('click', () => {
if (!window.confirm(tr('Desativar o MFA desta conta?'))) return;
attempt(status, () => withReauth(() => api('/api/auth/mfa/disable', { method: 'POST', body: {} })),
tr('MFA desativado.'));
attempt(status, async () => {
await withReauth(() => api('/api/auth/mfa/disable', { method: 'POST', body: {} }));
session.mfa_enabled = false;
showMfaState();
}, tr('MFA desativado.'));
});
}
@@ -366,7 +402,7 @@ export function initAccount(client, current) {
$('#mfaPolicy').textContent = admin
? 'O MFA é obrigatório para administradores. Cadastrar de novo troca o segredo e os códigos de recuperação.'
: 'O MFA é opcional para analistas. Cadastrar de novo troca o segredo e os códigos de recuperação.';
$('#mfaDisable').hidden = admin;
showMfaState();
$('#tokenScopeAdmin').hidden = !admin;
$('#tokenScopeAdmin').disabled = !admin;
$('#adminPanels').hidden = !admin;