fix(auth): Simplify user profile loading with direct API call
This commit is contained in:
parent
a4b9d013e1
commit
454fd5a8b0
1 changed files with 89 additions and 71 deletions
|
|
@ -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,7 +2932,6 @@
|
||||||
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 ||
|
||||||
|
|
@ -2941,22 +2942,29 @@
|
||||||
displayName.charAt(0) || "U"
|
displayName.charAt(0) || "U"
|
||||||
).toUpperCase();
|
).toUpperCase();
|
||||||
|
|
||||||
|
console.log("Updating user UI:", displayName, email);
|
||||||
|
|
||||||
if (userName) userName.textContent = displayName;
|
if (userName) userName.textContent = displayName;
|
||||||
if (userEmail) userEmail.textContent = email;
|
if (userEmail) userEmail.textContent = email;
|
||||||
if (userAvatar) {
|
if (userAvatar) {
|
||||||
const avatarSpan = userAvatar.querySelector("span");
|
const avatarSpan = userAvatar.querySelector("span");
|
||||||
if (avatarSpan) avatarSpan.textContent = initial;
|
if (avatarSpan) avatarSpan.textContent = initial;
|
||||||
}
|
}
|
||||||
if (userAvatarLarge)
|
if (userAvatarLarge) userAvatarLarge.textContent = initial;
|
||||||
userAvatarLarge.textContent = initial;
|
|
||||||
|
|
||||||
if (authAction) {
|
if (authAction) {
|
||||||
authAction.href = "#";
|
authAction.href = "#";
|
||||||
authAction.onclick = function (e) {
|
authAction.onclick = function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (window.GBAuth) {
|
fetch("/api/auth/logout", {
|
||||||
window.GBAuth.logout();
|
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)";
|
authAction.style.color = "var(--error)";
|
||||||
}
|
}
|
||||||
|
|
@ -2966,50 +2974,60 @@
|
||||||
'<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>';
|
'<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>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function loadUserProfile() {
|
||||||
|
var token =
|
||||||
|
localStorage.getItem("gb-access-token") ||
|
||||||
|
sessionStorage.getItem("gb-access-token");
|
||||||
|
if (!token) {
|
||||||
|
console.log("No auth token found");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen for auth service events
|
console.log(
|
||||||
if (window.GBAuth) {
|
"Loading user profile with token:",
|
||||||
window.GBAuth.on("userUpdated", updateUserUI);
|
token.substring(0, 10) + "...",
|
||||||
window.GBAuth.on("login", function () {
|
);
|
||||||
const user = window.GBAuth.getCurrentUser();
|
|
||||||
if (user) updateUserUI(user);
|
|
||||||
});
|
|
||||||
window.GBAuth.on("logout", function () {
|
|
||||||
window.location.href = "/auth/login.html";
|
|
||||||
});
|
|
||||||
|
|
||||||
// Load current user on page load
|
fetch("/api/auth/me", {
|
||||||
if (window.GBAuth.isAuthenticated()) {
|
headers: { Authorization: "Bearer " + token },
|
||||||
const user = window.GBAuth.getCurrentUser();
|
})
|
||||||
|
.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
|
} catch (e) {}
|
||||||
window.AuthService.fetchCurrentUser().then(
|
}
|
||||||
updateUserUI,
|
|
||||||
|
// 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>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue