Files
app/web/signin.js
T

27 lines
1.1 KiB
JavaScript

document.getElementById('signin').onsubmit = async event => {
event.preventDefault();
const form = event.currentTarget;
const status = document.getElementById('status');
const action = event.submitter.value;
const password = document.getElementById('password');
const payload = {name: document.getElementById('name').value, password: password.value};
password.value = '';
for (const button of form.querySelectorAll('button')) button.disabled = true;
try {
const response = await fetch('/api/auth/' + action, {
method: 'POST', credentials: 'same-origin',
headers: {'Content-Type': 'application/json'}, body: JSON.stringify(payload)
});
payload.password = '';
if (!response.ok) throw new Error();
location.assign('/');
} catch {
payload.password = '';
status.textContent = 'Sign-in unavailable. Check details and retry.';
for (const button of form.querySelectorAll('button')) button.disabled = false;
}
};
fetch('/api/session', {credentials: 'same-origin', cache: 'no-store'})
.then(response => { if (response.ok) location.replace('/'); }).catch(() => {});