f1tness_parser/frontend/js/login.js

26 lines
1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { login } from './auth.js';
import { alert, h } from './common.js';
const form = document.getElementById('login-form');
const status = document.getElementById('status');
/** Where to go after signing in: the page that sent us here, but only a path of this site */
function nextPage() {
const next = new URLSearchParams(location.search).get('next');
return next && next.startsWith('/') && !next.startsWith('//') ? next : 'index.html';
}
form.addEventListener('submit', async (event) => {
event.preventDefault();
const submit = form.querySelector('button');
submit.disabled = true;
try {
await login(form.elements.email.value, form.elements.password.value);
location.href = nextPage();
} catch (error) {
status.replaceChildren(h('div', { class: 'mb-3' }, alert('danger', error.status === 401
? 'Неверный email или пароль.'
: `Не удалось войти: ${error.message}`)));
submit.disabled = false;
}
});