Skip to content

fix(navbar): prevent undefined cloud profile URLs#962

Open
miacycle wants to merge 3 commits intomasterfrom
fix/cloud-auth-undefined-lookup
Open

fix(navbar): prevent undefined cloud profile URLs#962
miacycle wants to merge 3 commits intomasterfrom
fix/cloud-auth-undefined-lookup

Conversation

@miacycle
Copy link
Copy Markdown
Contributor

Summary

  • guard against missing Cloud profile avatar URLs in the docs navbar
  • fall back to the Cloud home page when the authenticated profile payload has no user ID
  • stop authenticated visitors from triggering requests to /cloud/undefined

Related Issue

Fixes #961

Avoid building Cloud avatar and profile URLs from missing profile fields so authenticated visitors do not trigger requests to /cloud/undefined.

Fixes #961

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Lee Calcote <lee.calcote@layer5.io>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the docs site navbar’s Cloud-authenticated user UI to avoid generating invalid/relative URLs (notably /cloud/undefined) when the profile payload is missing expected fields.

Changes:

  • Adds a cloudAppUrl constant and uses it to build the Cloud profile API request URL.
  • Introduces helpers to safely derive avatar_url and the user profile link, with fallbacks when missing.
  • Updates the authenticated navbar UI to avoid setting a background image when no avatar URL is present, and to fall back to the Cloud home page when no user ID is available.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 25, 2026

🚀 Preview deployment: https://layer5io.github.io/docs/pr-preview/pr-962/

Note: Preview may take a moment (GitHub Pages deployment in progress). Please wait and refresh. Track deployment here

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the navbar's user profile logic by centralizing the base URL and introducing helper functions for avatar and profile URL generation. The review feedback suggests further improvements, including using Hugo variables for the base URL to reduce duplication, adopting optional chaining for more idiomatic JavaScript, and adding safety checks for DOM elements to prevent potential runtime errors.

Comment thread layouts/partials/navbar.html Outdated
cur.href = "javascript: void(0)";
}

const cloudAppUrl = "https://cloud.layer5.io";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The URL https://cloud.layer5.io is hardcoded in multiple places within this file (lines 189, 192, and 263). Since this is a Hugo template, it is recommended to define this URL as a single variable (e.g., using a Hugo variable or a Site parameter) to ensure consistency and simplify future updates.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consolidated the Cloud base URL into shared Hugo variables in this partial so the sign-in, profile, and academy links all derive from one source.

Comment thread layouts/partials/navbar.html Outdated
Comment on lines +328 to +330
if (response && response.id !== undefined && response.id !== null && response.id !== "") {
return `${cloudAppUrl}/user/${encodeURIComponent(response.id)}`;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This condition is quite verbose. Using optional chaining and a truthiness check is more idiomatic in modern JavaScript and effectively covers null, undefined, and empty strings for the id field.

Suggested change
if (response && response.id !== undefined && response.id !== null && response.id !== "") {
return `${cloudAppUrl}/user/${encodeURIComponent(response.id)}`;
}
const userId = response?.id;
if (userId) {
return cloudAppUrl + "/user/" + encodeURIComponent(userId);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the helper to use an idiomatic optional-chaining check for the user ID before building the profile URL.

Comment thread layouts/partials/navbar.html Outdated
Comment on lines +340 to +342
const avatarContainer = document.querySelector('.avatar-container');
avatarContainer.style.backgroundImage = avatarUrl ? `url("${avatarUrl}")` : "none";
avatarContainer.style.backgroundSize = avatarUrl ? "cover" : "";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It is safer to verify that avatarContainer exists before attempting to modify its style properties. If the element is missing from the DOM, this script will throw a TypeError, which could prevent subsequent logic (like setting isUserAuthenticated) from executing.

Suggested change
const avatarContainer = document.querySelector('.avatar-container');
avatarContainer.style.backgroundImage = avatarUrl ? `url("${avatarUrl}")` : "none";
avatarContainer.style.backgroundSize = avatarUrl ? "cover" : "";
const avatarContainer = document.querySelector('.avatar-container');
if (avatarContainer) {
avatarContainer.style.backgroundImage = avatarUrl ? "url(\"" + avatarUrl + "\")" : "none";
avatarContainer.style.backgroundSize = avatarUrl ? "cover" : "";
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a guard around the avatar container before setting its styles so the auth UI does not throw if that element is absent.

leecalcote and others added 2 commits April 24, 2026 21:06
Use shared Hugo variables for Cloud URLs in the navbar so sign-in, profile, and academy links stay consistent while preserving the undefined-path fix.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Lee Calcote <lee.calcote@layer5.io>
Simplify the profile URL fallback logic and avoid touching avatar styles when the navbar avatar element is absent.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Lee Calcote <lee.calcote@layer5.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cloud-authenticated visitors experience an "undefined" lookup

3 participants