diff --git a/components/dashboard/src/Login.tsx b/components/dashboard/src/Login.tsx index 511587c4027044..f1a4dc5a9bc7e1 100644 --- a/components/dashboard/src/Login.tsx +++ b/components/dashboard/src/Login.tsx @@ -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; }; @@ -49,10 +69,19 @@ export const Login: FC = ({ 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]);