Archived
161 lines
6.5 KiB
JavaScript
161 lines
6.5 KiB
JavaScript
(() => {
|
|
const basePath = String(document.body.dataset.basePath || '').replace(/\/$/, '');
|
|
const appUrl = path => {
|
|
const value = String(path || '/');
|
|
if (!value.startsWith('/') || value.startsWith('//')) return value;
|
|
return `${basePath}${value}` || '/';
|
|
};
|
|
const $ = id => document.getElementById(id);
|
|
const error = message => {
|
|
const el = $('auth-error');
|
|
if (!el) return;
|
|
el.hidden = !message;
|
|
el.textContent = message || '';
|
|
};
|
|
const status = (id, message, kind='') => {
|
|
const el = $(id);
|
|
if (!el) return;
|
|
el.hidden = !message;
|
|
el.className = `operation-status${kind ? ` ${kind}` : ''}`;
|
|
el.textContent = message || '';
|
|
};
|
|
|
|
async function submit(endpoint, payload) {
|
|
const response = await fetch(appUrl(endpoint), {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify(payload),
|
|
});
|
|
const data = await response.json().catch(() => ({}));
|
|
if (!response.ok) throw new Error(data.error || 'Request failed');
|
|
return data;
|
|
}
|
|
|
|
$('setup-user-form')?.addEventListener('submit', async event => {
|
|
event.preventDefault();
|
|
error('');
|
|
const password = $('auth-password').value;
|
|
if (password !== $('auth-password-repeat').value) {
|
|
error(document.documentElement.lang === 'ru' ? 'Пароли не совпадают.' : 'Passwords do not match.');
|
|
return;
|
|
}
|
|
const button = event.currentTarget.querySelector('button[type="submit"]');
|
|
button.disabled = true;
|
|
try {
|
|
await submit('/api/auth/setup', {username: $('auth-username').value.trim(), email: $('auth-email')?.value.trim() || '', password});
|
|
location.replace(appUrl('/'));
|
|
} catch (e) {
|
|
error(e.message);
|
|
button.disabled = false;
|
|
}
|
|
});
|
|
|
|
$('login-form')?.addEventListener('submit', async event => {
|
|
event.preventDefault();
|
|
error('');
|
|
const form = event.currentTarget;
|
|
const button = form.querySelector('button[type="submit"]');
|
|
button.disabled = true;
|
|
try {
|
|
const data = await submit('/api/auth/login', {username: $('auth-username').value.trim(), password: $('auth-password').value});
|
|
if (data.mfa_required) {
|
|
form.hidden = true;
|
|
$('mfa-login-form').hidden = false;
|
|
$('auth-mfa-code').value = '';
|
|
button.disabled = false;
|
|
setTimeout(() => $('auth-mfa-code')?.focus(), 30);
|
|
return;
|
|
}
|
|
const next = new URLSearchParams(location.search).get('next');
|
|
location.replace(appUrl(next && next.startsWith('/') && !next.startsWith('//') ? next : '/'));
|
|
} catch (e) {
|
|
error(document.documentElement.lang === 'ru' && e.message === 'Invalid username or password' ? 'Неверный логин или пароль.' : e.message);
|
|
button.disabled = false;
|
|
}
|
|
});
|
|
|
|
$('mfa-login-form')?.addEventListener('submit', async event => {
|
|
event.preventDefault();
|
|
status('mfa-auth-error', '');
|
|
const button = event.currentTarget.querySelector('button[type="submit"]');
|
|
button.disabled = true;
|
|
try {
|
|
await submit('/api/auth/mfa', {code: $('auth-mfa-code').value.trim()});
|
|
const next = new URLSearchParams(location.search).get('next');
|
|
location.replace(appUrl(next && next.startsWith('/') && !next.startsWith('//') ? next : '/'));
|
|
} catch (e) {
|
|
let text = e.message;
|
|
if (document.documentElement.lang === 'ru') {
|
|
if (text === 'Invalid authentication code') text = 'Неверный код аутентификации.';
|
|
if (text === 'MFA challenge expired. Sign in again.') text = 'Время проверки MFA истекло. Войдите заново.';
|
|
if (text === 'Too many invalid MFA attempts. Sign in again.') text = 'Слишком много неверных кодов MFA. Войдите заново.';
|
|
}
|
|
status('mfa-auth-error', text, 'error');
|
|
button.disabled = false;
|
|
}
|
|
});
|
|
|
|
$('mfa-login-back')?.addEventListener('click', () => {
|
|
$('mfa-login-form').hidden = true;
|
|
$('login-form').hidden = false;
|
|
status('mfa-auth-error', '');
|
|
$('auth-mfa-code').value = '';
|
|
setTimeout(() => $('auth-password')?.focus(), 30);
|
|
});
|
|
|
|
$('password-reset-request-form')?.addEventListener('submit', async event => {
|
|
event.preventDefault();
|
|
error('');
|
|
const form = event.currentTarget;
|
|
const identifier = $('reset-identifier').value.trim();
|
|
const button = form.querySelector('button[type="submit"]');
|
|
button.disabled = true;
|
|
try {
|
|
await submit('/api/auth/password-reset/request', {identifier});
|
|
$('reset-confirm-identifier').value = identifier;
|
|
form.hidden = true;
|
|
$('password-reset-confirm-form').hidden = false;
|
|
setTimeout(() => $('reset-code')?.focus(), 30);
|
|
} catch (e) {
|
|
// This endpoint normally returns a generic success response even when delivery is unavailable.
|
|
error(e.message);
|
|
button.disabled = false;
|
|
}
|
|
});
|
|
|
|
$('password-reset-confirm-form')?.addEventListener('submit', async event => {
|
|
event.preventDefault();
|
|
status('reset-confirm-status', '');
|
|
const password = $('reset-password').value;
|
|
if (password !== $('reset-password-repeat').value) {
|
|
status('reset-confirm-status', document.documentElement.lang === 'ru' ? 'Пароли не совпадают.' : 'Passwords do not match.', 'error');
|
|
return;
|
|
}
|
|
const button = event.currentTarget.querySelector('button[type="submit"]');
|
|
button.disabled = true;
|
|
try {
|
|
await submit('/api/auth/password-reset/confirm', {
|
|
identifier: $('reset-confirm-identifier').value,
|
|
code: $('reset-code').value.trim(),
|
|
password,
|
|
});
|
|
status('reset-confirm-status', document.documentElement.lang === 'ru' ? 'Пароль изменён. Сейчас откроется страница входа.' : 'Password changed. Redirecting to sign in…', 'success');
|
|
setTimeout(() => location.replace(appUrl('/login')), 900);
|
|
} catch (e) {
|
|
const text = document.documentElement.lang === 'ru' && e.message === 'Invalid or expired reset code' ? 'Неверный или просроченный код.' : e.message;
|
|
status('reset-confirm-status', text, 'error');
|
|
button.disabled = false;
|
|
}
|
|
});
|
|
|
|
$('reset-request-again')?.addEventListener('click', () => {
|
|
$('password-reset-confirm-form').hidden = true;
|
|
$('password-reset-request-form').hidden = false;
|
|
$('reset-code').value = '';
|
|
$('reset-password').value = '';
|
|
$('reset-password-repeat').value = '';
|
|
status('reset-confirm-status', '');
|
|
setTimeout(() => $('reset-identifier')?.focus(), 30);
|
|
});
|
|
})();
|