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

+14 -4
View File
@@ -1,5 +1,5 @@
import { setLang, tr, LOC, currentLang, serverMessage } from './i18n.js';
import { initAccount, refreshAccount } from './account.js';
import { initAccount, refreshAccount, holdingOneTimeCodes } from './account.js';
const SVG_NS = 'http://www.w3.org/2000/svg';
const $ = (selector) => document.querySelector(selector);
@@ -1245,7 +1245,8 @@ function applyViewMeta() {
}
function goToLogin() {
location.replace('/login.html');
// Recovery codes on screen exist nowhere else: their "done" button leaves.
if (!holdingOneTimeCodes()) location.replace('/login.html');
// The page is leaving: callers must not go on to render a failure.
return new Promise(() => {});
}
@@ -1277,10 +1278,19 @@ function accountApi(path, { method = 'GET', body } = {}) {
}
async function logout() {
// On failure the session is still alive: looking signed out would be a lie.
const button = $('#logoutBtn');
const status = $('#logoutStatus');
status.hidden = true;
button.disabled = true;
try {
await fetchJson('/api/auth/logout', { method: 'POST' });
await goToLogin();
} catch (error) {
status.textContent = `${tr('Não foi possível sair; a sessão continua ativa.')} ${error.message}`;
status.hidden = false;
} finally {
location.replace('/login.html');
button.disabled = false;
}
}
@@ -1478,7 +1488,7 @@ async function init() {
await goToLogin();
return;
}
session = { username: auth.username, role: auth.role, csrf: auth.csrf };
session = { username: auth.username, role: auth.role, csrf: auth.csrf, mfa_enabled: auth.mfa_enabled };
$('#topUser').textContent = session.username;
$('#logoutBtn').addEventListener('click', logout);
if (session.role !== 'admin') {