26 lines
1 KiB
JavaScript
26 lines
1 KiB
JavaScript
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;
|
||
}
|
||
});
|