Keep login username prefilled via local storage
This commit is contained in:
@@ -23,6 +23,9 @@ function toast(msg, isError = false) {
|
||||
setTimeout(() => toastEl.classList.add("hidden"), 2000);
|
||||
}
|
||||
|
||||
const getSavedUsername = () => localStorage.getItem("last_username") || "";
|
||||
const setSavedUsername = (name) => localStorage.setItem("last_username", name);
|
||||
|
||||
function setAuthUI(isAuthed) {
|
||||
const main = document.querySelector("main");
|
||||
const statusBar = document.querySelector(".status-bar");
|
||||
@@ -35,19 +38,11 @@ function setAuthUI(isAuthed) {
|
||||
const adminCard = $("admin-card");
|
||||
if (adminCard) adminCard.classList.add("hidden");
|
||||
const loginUser = $("login-username");
|
||||
const cachedUser = getCookie("player_username");
|
||||
const cachedUser = getSavedUsername();
|
||||
if (loginUser && cachedUser) loginUser.value = cachedUser;
|
||||
}
|
||||
}
|
||||
|
||||
function getCookie(name) {
|
||||
const cookie = document.cookie
|
||||
?.split(";")
|
||||
.map(c => c.trim())
|
||||
.find(c => c.startsWith(name + "="));
|
||||
return cookie ? decodeURIComponent(cookie.split("=").slice(1).join("=")) : null;
|
||||
}
|
||||
|
||||
function setAuthMode(mode) {
|
||||
state.authMode = mode;
|
||||
document.querySelectorAll(".auth-form").forEach(form => {
|
||||
@@ -261,6 +256,7 @@ function setupHandlers() {
|
||||
if (!username || !password) return toast("Username and password required", true);
|
||||
try {
|
||||
await api.login({ username, password });
|
||||
setSavedUsername(username);
|
||||
state.isAuthenticated = true;
|
||||
setAuthUI(true);
|
||||
await refreshPhaseData();
|
||||
@@ -283,6 +279,7 @@ function setupHandlers() {
|
||||
if (!username || !password) return toast("Username and password required", true);
|
||||
try {
|
||||
await api.register({ username, password, displayName, adminKey });
|
||||
setSavedUsername(username);
|
||||
state.isAuthenticated = true;
|
||||
setAuthUI(true);
|
||||
await refreshPhaseData();
|
||||
@@ -335,6 +332,7 @@ function setupHandlers() {
|
||||
if (logoutBtn) {
|
||||
logoutBtn.addEventListener("click", async (e) => {
|
||||
e.preventDefault();
|
||||
const lastUser = state.me?.username;
|
||||
try {
|
||||
await api.logout();
|
||||
} catch (err) {
|
||||
@@ -343,6 +341,11 @@ function setupHandlers() {
|
||||
clearUserState();
|
||||
state.isAuthenticated = false;
|
||||
setAuthUI(false);
|
||||
if (lastUser) {
|
||||
setSavedUsername(lastUser);
|
||||
const loginUser = $("login-username");
|
||||
if (loginUser) loginUser.value = lastUser;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user