Skip to content
Source
Sign inSign up
Open History to see the last commit

providerAuthReturnUrl.ts

33 lines1.1 KB

1import { isLoopbackHost } from "./preview.ts";
2​
3export function providerAuthReturnUrl(value: string | undefined): string | undefined {
4 if (!value) return undefined;
5 try {
6 const url = new URL(value);
7 const desktop = ["t3code:", "t3code-dev:"].includes(url.protocol) && url.host === "app";
8 const web =
9 ["http:", "https:"].includes(url.protocol) &&
10 (isLoopbackHost(url.hostname) || url.origin === "https://app.t3.codes");
11 if (
12 url.username ||
13 url.password ||
14 (!desktop && !web) ||
15 (url.pathname !== "/welcome" &&
16 url.pathname !== "/settings" &&
17 !url.pathname.startsWith("/settings/"))
18 )
19 return undefined;
20 for (const key of Array.from(url.searchParams.keys())) {
21 if (
22 url.pathname === "/welcome" ||
23 !["machine", "project", "checkout", "environmentId", "instanceId"].includes(key)
24 ) {
25 url.searchParams.delete(key);
26 }
27 }
28 if (url.pathname !== "/welcome" || !/^#agents:[\w-]+$/u.test(url.hash)) url.hash = "";
29 return url.toString();
30 } catch {
31 return undefined;
32 }