склади и инструмент готовы
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
// auth.js
|
||||
import { setCookie } from '/static/js/cookies.js';
|
||||
import { apiRequest } from '/static/js/api.js';
|
||||
|
||||
const form = document.getElementById('loginForm');
|
||||
const loginInput = document.getElementById('loginInput');
|
||||
const passwordInput = document.getElementById('passwordInput');
|
||||
const submitBtn = document.getElementById('submitBtn');
|
||||
const formError = document.getElementById('formError');
|
||||
|
||||
function showError(msg) {
|
||||
formError.hidden = false;
|
||||
formError.textContent = msg;
|
||||
}
|
||||
|
||||
function clearError() {
|
||||
formError.hidden = true;
|
||||
formError.textContent = '';
|
||||
}
|
||||
|
||||
async function getUserEndpoint() {
|
||||
const meta = document.querySelector('meta[name="userAuth-endpoint"]');
|
||||
return meta ? meta.getAttribute('content') : '/user';
|
||||
}
|
||||
|
||||
form.addEventListener('submit', async (ev) => {
|
||||
ev.preventDefault();
|
||||
clearError();
|
||||
submitBtn.disabled = true;
|
||||
submitBtn.textContent = 'Входим...';
|
||||
|
||||
const login = loginInput.value.trim();
|
||||
const password = passwordInput.value;
|
||||
|
||||
if (!login || !password) {
|
||||
showError('Введите логин и пароль');
|
||||
submitBtn.disabled = false;
|
||||
submitBtn.textContent = 'Войти';
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const url = await getUserEndpoint();
|
||||
// Отправляем данные
|
||||
const resp = await apiRequest(url, { login, password });
|
||||
|
||||
// Пример ожидаемого формата:
|
||||
// { "status": "ok" | "error", "user": {...}, "access": {...} }
|
||||
if (!resp || typeof resp !== 'object') {
|
||||
throw new Error('Некорректный ответ от сервера');
|
||||
}
|
||||
|
||||
if (resp.status && (resp.status === 'error' || resp.status === 'fail')) {
|
||||
// Ошибка авторизации
|
||||
showError(resp.message || 'Неправильный логин или пароль');
|
||||
submitBtn.disabled = false;
|
||||
submitBtn.textContent = 'Войти';
|
||||
return;
|
||||
}
|
||||
|
||||
// Если статус == success / ok / authenticated и есть user + access — считаем успешной
|
||||
const okStatuses = new Set(['ok', 'success', 'authenticated']);
|
||||
const isOk = resp.status && okStatuses.has(String(resp.status).toLowerCase());
|
||||
|
||||
// fallback: если resp.user или resp.access присутствуют — считаем ок
|
||||
const hasData = resp.user && typeof resp.user === 'object' && resp.access && typeof resp.access === 'object';
|
||||
|
||||
if (!isOk && !hasData) {
|
||||
showError('Авторизация не выполнена: пустой ответ');
|
||||
submitBtn.disabled = false;
|
||||
submitBtn.textContent = 'Войти';
|
||||
return;
|
||||
}
|
||||
await setCookie('toolbox_user', JSON.stringify(resp.user));
|
||||
await setCookie('toolbox_access', JSON.stringify(resp.access));
|
||||
|
||||
window.location.href = '/'; // или другой URL
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
showError(err.message || 'Ошибка при авторизации');
|
||||
submitBtn.disabled = false;
|
||||
submitBtn.textContent = 'Войти';
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user