Fix login flow after rollback
This commit is contained in:
93
web/app.js
93
web/app.js
@@ -155,7 +155,7 @@ $('#loginForm').addEventListener('submit', async e=>{
|
||||
e.preventDefault();
|
||||
$('#loginError').textContent = '';
|
||||
const fd = new FormData(e.currentTarget);
|
||||
try { await api('/api/login', {method:'POST', body: JSON.stringify(Object.fromEntries(fd))}); await init(); }
|
||||
try { await api('/api/login', {method:'POST', body: JSON.stringify(Object.fromEntries(fd))}); window.location.href = '/'; return; }
|
||||
catch(err){ $('#loginError').textContent = err.message; }
|
||||
});
|
||||
$('#logoutBtn').addEventListener('click', async()=>{ await api('/api/logout',{method:'POST',body:'{}'}); location.reload(); });
|
||||
@@ -221,94 +221,3 @@ $('#rejectPhotoBtn').addEventListener('click', async()=>{
|
||||
});
|
||||
|
||||
init();
|
||||
|
||||
function renderAccountView() {
|
||||
const main = document.querySelector("#content") || document.querySelector("main") || document.querySelector(".main");
|
||||
if (!main) return;
|
||||
|
||||
const user = state.user || {};
|
||||
main.innerHTML = `
|
||||
<div class="header">
|
||||
<h1>Moje konto</h1>
|
||||
<p>Zmień swoje hasło i sprawdź informacje o koncie.</p>
|
||||
</div>
|
||||
|
||||
<div class="card account-card" style="padding:24px;margin-top:24px;">
|
||||
<h2>Dane konta</h2>
|
||||
<div class="list-row"><strong>Login</strong><span>${escapeHtml(user.username || '')}</span></div>
|
||||
<div class="list-row"><strong>Nazwa</strong><span>${escapeHtml(user.display_name || '')}</span></div>
|
||||
<div class="list-row"><strong>Rola</strong><span>${escapeHtml(user.role || '')}</span></div>
|
||||
</div>
|
||||
|
||||
<div class="card account-card" style="padding:24px;margin-top:18px;">
|
||||
<h2>Zmiana hasła</h2>
|
||||
<form id="changePasswordForm">
|
||||
<div class="field">
|
||||
<label>Obecne hasło</label>
|
||||
<input name="current_password" type="password" required />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Nowe hasło</label>
|
||||
<input name="new_password" type="password" minlength="8" required />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Powtórz nowe hasło</label>
|
||||
<input name="repeat_password" type="password" minlength="8" required />
|
||||
</div>
|
||||
<div class="form-error" id="changePasswordError"></div>
|
||||
<button class="primary-btn" type="submit" style="margin-top:16px;">Zmień hasło</button>
|
||||
</form>
|
||||
</div>
|
||||
`;
|
||||
|
||||
document.querySelector("#changePasswordForm")?.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const fd = new FormData(e.currentTarget);
|
||||
const current_password = fd.get("current_password");
|
||||
const new_password = fd.get("new_password");
|
||||
const repeat_password = fd.get("repeat_password");
|
||||
const err = document.querySelector("#changePasswordError");
|
||||
|
||||
if (new_password !== repeat_password) {
|
||||
err.textContent = "Nowe hasła nie są takie same.";
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await api("/api/me/password", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ current_password, new_password })
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
err.textContent = res.error || "Nie udało się zmienić hasła.";
|
||||
return;
|
||||
}
|
||||
|
||||
err.style.color = "#16a34a";
|
||||
err.textContent = "Hasło zostało zmienione.";
|
||||
e.currentTarget.reset();
|
||||
} catch (ex) {
|
||||
err.textContent = "Błąd połączenia z serwerem.";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function escapeHtml(value) {
|
||||
return String(value ?? "")
|
||||
.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll('"', """)
|
||||
.replaceAll("'", "'");
|
||||
}
|
||||
|
||||
document.addEventListener("click", (e) => {
|
||||
const btn = e.target.closest("[data-view='account']");
|
||||
if (!btn) return;
|
||||
e.preventDefault();
|
||||
state.currentView = "account";
|
||||
document.querySelectorAll(".nav-btn").forEach(b => b.classList.remove("active"));
|
||||
btn.classList.add("active");
|
||||
renderAccountView();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user