Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions frontend/js/user/profile-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
10 changes: 10 additions & 0 deletions frontend/js/user/utils.js
Original file line number Diff line number Diff line change
@@ -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();
}
1 change: 1 addition & 0 deletions frontend/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="current-user" content="__USERNAME__" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title id="page-title">CodePVG</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
Expand Down
35 changes: 22 additions & 13 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,26 @@ app.use((req, res, next) => {

// 4. HTML page routes — inject per-request nonce into __NONCE__ placeholders
const htmlCache = {};
function serveHtml(res, filePath) {
if (htmlCache[filePath]) {
const html = htmlCache[filePath].replace(/__NONCE__/g, res.locals.nonce);
return res.type("html").send(html);
}

fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
function serveHtml(res, filePath, replacements = {}) {
let template = htmlCache[filePath];

if (!template) {
try {
template = fs.readFileSync(filePath, "utf8");
htmlCache[filePath] = template;
} catch (err) {
return res.status(500).send("Error loading page");
}
htmlCache[filePath] = data;
const html = data.replace(/__NONCE__/g, res.locals.nonce);
res.type("html").send(html);
});
}

let html = template.replace(/__NONCE__/g, res.locals.nonce);

for (const [key, value] of Object.entries(replacements)) {
const regex = new RegExp(key, "g");
html = html.replace(regex, value);
}

res.type("html").send(html);
}

/* HOME ROUTES */
Expand Down Expand Up @@ -133,7 +139,10 @@ app.get("/uptime", (req, res) => {
});

app.get("/user/:username", (req, res) => {
serveHtml(res, path.join(__dirname, "frontend", "user.html"));
const username = req.params.username;
serveHtml(res, path.join(__dirname, "frontend", "user.html"), {
__USERNAME__: username,
});
});

// ---- Rate limiter for API endpoint ----
Expand Down
Loading