Skip to content

Commit

Permalink
Fix login URL host parsing regression (#20265)
Browse files Browse the repository at this point in the history
* Fix login URL host parsing regression

* fallback with prefixes
  • Loading branch information
filiptronicek authored Oct 2, 2024
1 parent ca933cb commit 2b6d952
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions components/dashboard/src/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ export function hasLoggedInBefore() {
return GitpodCookie.isPresent(document.cookie);
}

const SEGMENT_SEPARATOR = "/";
const getContextUrlFromHash = (input: string): URL | undefined => {
if (typeof URL.canParse !== "function") {
return undefined;
}
if (URL.canParse(input)) {
return new URL(input);
}

const chunks = input.split(SEGMENT_SEPARATOR);
for (const chunk of chunks) {
input = input.replace(`${chunk}${SEGMENT_SEPARATOR}`, "");
if (URL.canParse(input)) {
return new URL(input);
}
}

return undefined;
};

type LoginProps = {
onLoggedIn?: () => void;
};
Expand All @@ -49,10 +69,19 @@ export const Login: FC<LoginProps> = ({ onLoggedIn }) => {
const enterprise = !!authProviders.data && authProviders.data.length === 0;

useEffect(() => {
if (urlHash.length > 0) {
const url = new URL(urlHash);
setHostFromContext(url.host);
setRepoPathname(url.pathname);
try {
if (urlHash.length > 0) {
const url = new URL(urlHash);
setHostFromContext(url.host);
setRepoPathname(url.pathname);
}
} catch (error) {
// hash is not a valid URL, try to extract the context URL when there are parts like env vars or other prefixes
const contextUrl = getContextUrlFromHash(urlHash);
if (contextUrl) {
setHostFromContext(contextUrl.host);
setRepoPathname(contextUrl.pathname);
}
}
}, [urlHash]);

Expand Down

0 comments on commit 2b6d952

Please sign in to comment.