fix(auth): Simplify user profile loading with direct API call

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2026-01-07 07:09:29 -03:00
parent a4b9d013e1
commit 454fd5a8b0

View file

@ -2918,9 +2918,11 @@
}); });
}); });
// User Profile Loading - Integrates with GBAuth service // User Profile Loading
(function () { (function () {
function updateUserUI(user) { function updateUserUI(user) {
if (!user) return;
const userName = document.getElementById("userName"); const userName = document.getElementById("userName");
const userEmail = document.getElementById("userEmail"); const userEmail = document.getElementById("userEmail");
const userAvatar = document.getElementById("userAvatar"); const userAvatar = document.getElementById("userAvatar");
@ -2930,86 +2932,102 @@
const authText = document.getElementById("authText"); const authText = document.getElementById("authText");
const authIcon = document.getElementById("authIcon"); const authIcon = document.getElementById("authIcon");
if (user && user.email) { const displayName =
const displayName = user.display_name ||
user.display_name || user.first_name ||
user.first_name || user.username ||
user.username || "User";
"User"; const email = user.email || "";
const email = user.email || ""; const initial = (
const initial = ( displayName.charAt(0) || "U"
displayName.charAt(0) || "U" ).toUpperCase();
).toUpperCase();
if (userName) userName.textContent = displayName; console.log("Updating user UI:", displayName, email);
if (userEmail) userEmail.textContent = email;
if (userAvatar) {
const avatarSpan = userAvatar.querySelector("span");
if (avatarSpan) avatarSpan.textContent = initial;
}
if (userAvatarLarge)
userAvatarLarge.textContent = initial;
if (authAction) { if (userName) userName.textContent = displayName;
authAction.href = "#"; if (userEmail) userEmail.textContent = email;
authAction.onclick = function (e) { if (userAvatar) {
e.preventDefault(); const avatarSpan = userAvatar.querySelector("span");
if (window.GBAuth) { if (avatarSpan) avatarSpan.textContent = initial;
window.GBAuth.logout(); }
} if (userAvatarLarge) userAvatarLarge.textContent = initial;
};
authAction.style.color = "var(--error)"; if (authAction) {
} authAction.href = "#";
if (authText) authText.textContent = "Sign out"; authAction.onclick = function (e) {
if (authIcon) { e.preventDefault();
authIcon.innerHTML = fetch("/api/auth/logout", {
'<path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path><polyline points="16 17 21 12 16 7"></polyline><line x1="21" y1="12" x2="9" y2="12"></line>'; method: "POST",
} }).finally(function () {
localStorage.removeItem("gb-access-token");
localStorage.removeItem("gb-refresh-token");
localStorage.removeItem("gb-user-data");
sessionStorage.removeItem("gb-access-token");
window.location.href = "/auth/login.html";
});
};
authAction.style.color = "var(--error)";
}
if (authText) authText.textContent = "Sign out";
if (authIcon) {
authIcon.innerHTML =
'<path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path><polyline points="16 17 21 12 16 7"></polyline><line x1="21" y1="12" x2="9" y2="12"></line>';
} }
} }
// Listen for auth service events function loadUserProfile() {
if (window.GBAuth) { var token =
window.GBAuth.on("userUpdated", updateUserUI); localStorage.getItem("gb-access-token") ||
window.GBAuth.on("login", function () { sessionStorage.getItem("gb-access-token");
const user = window.GBAuth.getCurrentUser(); if (!token) {
if (user) updateUserUI(user); console.log("No auth token found");
}); return;
window.GBAuth.on("logout", function () { }
window.location.href = "/auth/login.html";
});
// Load current user on page load console.log(
if (window.GBAuth.isAuthenticated()) { "Loading user profile with token:",
const user = window.GBAuth.getCurrentUser(); token.substring(0, 10) + "...",
);
fetch("/api/auth/me", {
headers: { Authorization: "Bearer " + token },
})
.then(function (res) {
if (!res.ok) throw new Error("Not authenticated");
return res.json();
})
.then(function (user) {
console.log("User profile loaded:", user);
updateUserUI(user);
localStorage.setItem(
"gb-user-data",
JSON.stringify(user),
);
})
.catch(function (err) {
console.log("Failed to load user profile:", err);
});
}
// Try to load cached user first
var cachedUser = localStorage.getItem("gb-user-data");
if (cachedUser) {
try {
var user = JSON.parse(cachedUser);
if (user && user.email) { if (user && user.email) {
updateUserUI(user); updateUserUI(user);
} else {
// Fetch from API if not cached
window.AuthService.fetchCurrentUser().then(
updateUserUI,
);
} }
} } catch (e) {}
}
// Always fetch fresh user data
if (document.readyState === "loading") {
document.addEventListener(
"DOMContentLoaded",
loadUserProfile,
);
} else { } else {
// Fallback: wait for GBAuth to be available loadUserProfile();
document.addEventListener("DOMContentLoaded", function () {
setTimeout(function () {
if (
window.GBAuth &&
window.GBAuth.isAuthenticated()
) {
const user = window.GBAuth.getCurrentUser();
if (user) {
updateUserUI(user);
} else {
window.AuthService.fetchCurrentUser().then(
updateUserUI,
);
}
}
}, 100);
});
} }
})(); })();
</script> </script>