diff --git a/frontend/js/user/profile-controller.js b/frontend/js/user/profile-controller.js index 8f229e4f..5b2e63bc 100644 --- a/frontend/js/user/profile-controller.js +++ b/frontend/js/user/profile-controller.js @@ -4,19 +4,14 @@ import { loadGoalSetter } from "./goal-setter.js"; import { fetchUserData } from "./historical-graphs.js"; import { loadLeaderboardRanks } from "./ranks.js"; import { loadStreakData } from "./streak.js"; - -function getUsername() { - const pathSegments = window.location.pathname.split("/"); - return ( - pathSegments[pathSegments.length - 1] || - pathSegments[pathSegments.length - 2] || - "" - ); -} +import { getCurrentUsername } from "./utils.js"; async function initProfile() { - const username = getUsername(); - if (!username) return; + const username = getCurrentUsername(); + if (!username) { + console.error("Critical error: Username meta tag is missing."); + return; + } // Set header display variables prior to fetch if elements exist const usernameHeading = document.getElementById("username-display"); diff --git a/frontend/js/user/utils.js b/frontend/js/user/utils.js new file mode 100644 index 00000000..1fe4b7ac --- /dev/null +++ b/frontend/js/user/utils.js @@ -0,0 +1,10 @@ +export function getCurrentUsername() { + const metaTag = document.querySelector('meta[name="current-user"]'); + + if (!metaTag || !metaTag.content) { + console.warn("Current user meta tag is missing or empty."); + return null; + } + + return metaTag.content.trim(); +} diff --git a/frontend/user.html b/frontend/user.html index fa8d49b9..dd642ceb 100644 --- a/frontend/user.html +++ b/frontend/user.html @@ -2,6 +2,7 @@
+