Files
t3code/apps/web/src/pairingUrl.ts
b7559c4678 Implement server auth bootstrap and pairing flow (#1768)
Co-authored-by: codex <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: cursor[bot] <206951365+cursor[bot]@users.noreply.github.com>
2026-04-09 10:18:30 -07:00

34 lines
1.1 KiB
TypeScript

const PAIRING_TOKEN_PARAM = "token";
function readHashParams(url: URL): URLSearchParams {
return new URLSearchParams(url.hash.startsWith("#") ? url.hash.slice(1) : url.hash);
}
export function getPairingTokenFromUrl(url: URL): string | null {
const hashToken = readHashParams(url).get(PAIRING_TOKEN_PARAM)?.trim() ?? "";
if (hashToken.length > 0) {
return hashToken;
}
const searchToken = url.searchParams.get(PAIRING_TOKEN_PARAM)?.trim() ?? "";
return searchToken.length > 0 ? searchToken : null;
}
export function stripPairingTokenFromUrl(url: URL): URL {
const next = new URL(url.toString());
const hashParams = readHashParams(next);
if (hashParams.has(PAIRING_TOKEN_PARAM)) {
hashParams.delete(PAIRING_TOKEN_PARAM);
next.hash = hashParams.toString();
}
next.searchParams.delete(PAIRING_TOKEN_PARAM);
return next;
}
export function setPairingTokenOnUrl(url: URL, credential: string): URL {
const next = new URL(url.toString());
next.searchParams.delete(PAIRING_TOKEN_PARAM);
next.hash = new URLSearchParams([[PAIRING_TOKEN_PARAM, credential]]).toString();
return next;
}