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);
|
setTimeout(() => toastEl.classList.add("hidden"), 2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getSavedUsername = () => localStorage.getItem("last_username") || "";
|
||||||
|
const setSavedUsername = (name) => localStorage.setItem("last_username", name);
|
||||||
|
|
||||||
function setAuthUI(isAuthed) {
|
function setAuthUI(isAuthed) {
|
||||||
const main = document.querySelector("main");
|
const main = document.querySelector("main");
|
||||||
const statusBar = document.querySelector(".status-bar");
|
const statusBar = document.querySelector(".status-bar");
|
||||||
@@ -35,19 +38,11 @@ function setAuthUI(isAuthed) {
|
|||||||
const adminCard = $("admin-card");
|
const adminCard = $("admin-card");
|
||||||
if (adminCard) adminCard.classList.add("hidden");
|
if (adminCard) adminCard.classList.add("hidden");
|
||||||
const loginUser = $("login-username");
|
const loginUser = $("login-username");
|
||||||
const cachedUser = getCookie("player_username");
|
const cachedUser = getSavedUsername();
|
||||||
if (loginUser && cachedUser) loginUser.value = cachedUser;
|
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) {
|
function setAuthMode(mode) {
|
||||||
state.authMode = mode;
|
state.authMode = mode;
|
||||||
document.querySelectorAll(".auth-form").forEach(form => {
|
document.querySelectorAll(".auth-form").forEach(form => {
|
||||||
@@ -261,6 +256,7 @@ function setupHandlers() {
|
|||||||
if (!username || !password) return toast("Username and password required", true);
|
if (!username || !password) return toast("Username and password required", true);
|
||||||
try {
|
try {
|
||||||
await api.login({ username, password });
|
await api.login({ username, password });
|
||||||
|
setSavedUsername(username);
|
||||||
state.isAuthenticated = true;
|
state.isAuthenticated = true;
|
||||||
setAuthUI(true);
|
setAuthUI(true);
|
||||||
await refreshPhaseData();
|
await refreshPhaseData();
|
||||||
@@ -283,6 +279,7 @@ function setupHandlers() {
|
|||||||
if (!username || !password) return toast("Username and password required", true);
|
if (!username || !password) return toast("Username and password required", true);
|
||||||
try {
|
try {
|
||||||
await api.register({ username, password, displayName, adminKey });
|
await api.register({ username, password, displayName, adminKey });
|
||||||
|
setSavedUsername(username);
|
||||||
state.isAuthenticated = true;
|
state.isAuthenticated = true;
|
||||||
setAuthUI(true);
|
setAuthUI(true);
|
||||||
await refreshPhaseData();
|
await refreshPhaseData();
|
||||||
@@ -335,6 +332,7 @@ function setupHandlers() {
|
|||||||
if (logoutBtn) {
|
if (logoutBtn) {
|
||||||
logoutBtn.addEventListener("click", async (e) => {
|
logoutBtn.addEventListener("click", async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
const lastUser = state.me?.username;
|
||||||
try {
|
try {
|
||||||
await api.logout();
|
await api.logout();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -343,6 +341,11 @@ function setupHandlers() {
|
|||||||
clearUserState();
|
clearUserState();
|
||||||
state.isAuthenticated = false;
|
state.isAuthenticated = false;
|
||||||
setAuthUI(false);
|
setAuthUI(false);
|
||||||
|
if (lastUser) {
|
||||||
|
setSavedUsername(lastUser);
|
||||||
|
const loginUser = $("login-username");
|
||||||
|
if (loginUser) loginUser.value = lastUser;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user