feat(вход): TOTP и recovery

This commit is contained in:
anor
2026-11-06 23:14:30 +10:00
parent 6d45cf7c99
commit 4051c9eb3d
7 changed files with 309 additions and 5 deletions
+20 -1
View File
@@ -420,6 +420,7 @@ html { color-scheme: light; }
<div id="login-error" role="alert"></div>
<input type="text" id="login-user" aria-describedby="dev-banner" placeholder="Email или логин" aria-label="Email или логин" autocomplete="username" required>
<input type="password" id="login-pass" placeholder="Пароль" aria-label="Пароль" autocomplete="current-password" required>
<input type="text" id="login-totp" placeholder="Код 2FA или recovery-код" aria-label="Код 2FA" autocomplete="one-time-code" inputmode="numeric" style="display:none">
<button type="submit">Войти</button>
<p class="register-link">Нет аккаунта? <a href="/register">Зарегистрироваться</a></p>
<p class="register-link"><a href="#" id="forgot-link" onclick="return openReset(event)">Забыли пароль?</a></p>
@@ -1154,19 +1155,31 @@ async function doLogin(e) {
e.preventDefault();
const user = document.getElementById('login-user').value;
const pass = document.getElementById('login-pass').value;
const totpInput = document.getElementById('login-totp');
const body = {username: user, password: pass};
if (totpInput.style.display !== 'none' && totpInput.value.trim()) {
const v = totpInput.value.trim();
if (/^\d{6}$/.test(v)) body.code = v; else body.recovery = v;
}
hideBox('login-notice');
причинаВыхода = null; // дальше говорит ответ на этот вход
try {
const r = await fetch(API + '/api/login', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({username: user, password: pass})
body: JSON.stringify(body)
});
if (!r.ok) {
showBox('login-error', await serverReason(r, 'Не удалось войти. Проверьте почту и пароль.'));
return false;
}
const data = await r.json();
if (data.totp_required) {
totpInput.style.display = '';
totpInput.focus();
showBox('login-notice', 'Введите код из приложения-аутентификатора.');
return false;
}
token = data.token;
localStorage.setItem('token', token);
if (data.refresh_token) localStorage.setItem('refresh_token', data.refresh_token);
@@ -1274,6 +1287,12 @@ async function tryRefreshToken() {
return false;
}
const data = await r.json();
if (data.totp_required) {
totpInput.style.display = '';
totpInput.focus();
showBox('login-notice', 'Введите код из приложения-аутентификатора.');
return false;
}
token = data.token;
localStorage.setItem('token', token);
return true;