Open History to see the last commit
1import {
2 buildConnectClerkAuthorizeUrl,
3 connectCallbackUrl,
4 CONNECT_OAUTH_SCOPES,
5 type ConnectAuthorizeRequest,
6} from "@t3tools/shared/connectAuth";
7import { clerkFrontendApiUrlFromPublishableKey } from "@t3tools/shared/relayAuth";
8
9import { configuredHostedAppUrl, isHostedStaticApp } from "../hostedPairing";
10import { hasCloudPublicConfig, resolveCloudPublicConfig, trimNonEmpty } from "./publicConfig";
11
12const CONNECT_CLI_AUTH_STATE_STORAGE_KEY = "t3code-connect-cli-auth-state";
13
14export function resolveConnectCliOAuthClientId(): string | null {
15 return trimNonEmpty(import.meta.env.VITE_CLERK_CLI_OAUTH_CLIENT_ID as string | undefined);
16}
17
18export function hasConnectCliAuthConfig(): boolean {
19 return Boolean(
20 resolveCloudPublicConfig().clerkPublishableKey && resolveConnectCliOAuthClientId(),
21 );
22}
23
24/**
25 * Gate for the /connect routes: the CLI handshake only exists on the hosted
26 * deployment (the same bundle ships inside local instances) and needs the
27 * Clerk CLI OAuth client configured at build time.
28 */
29export function connectCliAuthRoutesEnabled(): boolean {
30 return isHostedStaticApp() && hasCloudPublicConfig() && hasConnectCliAuthConfig();
31}
32
33/**
34 * Builds the Clerk authorize URL for a CLI-initiated connect request. The
35 * state is mirrored into sessionStorage so the callback page can verify the
36 * response matches a request this browser actually started.
37 */
38export function buildConnectCliClerkAuthorizeUrl(request: ConnectAuthorizeRequest): string | null {
39 const { clerkPublishableKey } = resolveCloudPublicConfig();
40 const clientId = resolveConnectCliOAuthClientId();
41 if (!clerkPublishableKey || !clientId) {
42 return null;
43 }
44 return buildConnectClerkAuthorizeUrl({
45 authorizationEndpoint: `${clerkFrontendApiUrlFromPublishableKey(clerkPublishableKey)}/oauth/authorize`,
46 clientId,
47 redirectUri: connectCallbackUrl(configuredHostedAppUrl()),
48 scopes: CONNECT_OAUTH_SCOPES,
49 state: request.state,
50 challenge: request.challenge,
51 });
52}
53
54export function rememberConnectCliAuthState(state: string): void {
55 try {
56 window.sessionStorage.setItem(CONNECT_CLI_AUTH_STATE_STORAGE_KEY, state);
57 } catch {
58 // Session storage can be unavailable (e.g. blocked). The callback page
59 // then falls back to trusting the state Clerk echoed back.
60 }
61}
62
63/**
64 * Read-only on purpose: this runs during render, where a removal would be
65 * consumed by React's double-invoked/discarded renders (StrictMode) and
66 * silently disable the state check. The value is not a secret and is
67 * overwritten by the next /connect visit.
68 */
69export function readConnectCliAuthState(): string | null {
70 try {
71 return window.sessionStorage.getItem(CONNECT_CLI_AUTH_STATE_STORAGE_KEY);
72 } catch {
73 return null;
74 }
75}
76
77export interface ConnectCliCallbackResult {
78 readonly code: string;
79 readonly state: string;
80}
81
82export function readConnectCliCallbackResult(
83 url: URL = new URL(window.location.href),
84): ConnectCliCallbackResult | null {
85 const code = url.searchParams.get("code")?.trim() ?? "";
86 const state = url.searchParams.get("state")?.trim() ?? "";
87 if (!code || !state) {
88 return null;
89 }
90 return { code, state };
91}