@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 48 lines 1.73 KB
Raw
1 import {
2 buildConnectClerkAuthorizeUrl,
3 connectLoopbackRedirectUri,
4 CONNECT_OAUTH_SCOPES,
5 type ConnectAuthorizeRequest,
6 } from "@t3tools/shared/connectAuth";
7 import { clerkFrontendApiUrlFromPublishableKey } from "@t3tools/shared/relayAuth";
8
9 import { isHostedStaticApp } from "../hostedPairing";
10 import { hasCloudPublicConfig, resolveCloudPublicConfig, trimNonEmpty } from "./publicConfig";
11
12 function resolveConnectCliOAuthClientId(): string | null {
13 return trimNonEmpty(import.meta.env.VITE_CLERK_CLI_OAUTH_CLIENT_ID as string | undefined);
14 }
15
16 function hasConnectCliAuthConfig(): boolean {
17 return Boolean(
18 resolveCloudPublicConfig().clerkPublishableKey && resolveConnectCliOAuthClientId(),
19 );
20 }
21
22 export function connectCliAuthRoutesEnabled(): boolean {
23 return isHostedStaticApp() && hasCloudPublicConfig() && hasConnectCliAuthConfig();
24 }
25
26 export function buildConnectCliClerkAuthorizeUrl(request: ConnectAuthorizeRequest): string | null {
27 const { clerkPublishableKey } = resolveCloudPublicConfig();
28 const clientId = resolveConnectCliOAuthClientId();
29 if (!clerkPublishableKey || !clientId) {
30 return null;
31 }
32 return buildConnectClerkAuthorizeUrl({
33 authorizationEndpoint: `${clerkFrontendApiUrlFromPublishableKey(clerkPublishableKey)}/oauth/authorize`,
34 clientId,
35 redirectUri: connectLoopbackRedirectUri(request.loopbackPort),
36 scopes: CONNECT_OAUTH_SCOPES,
37 state: request.state,
38 challenge: request.challenge,
39 });
40 }
41
42 /** Force a new document: fragment revisits do not reload, and Clerk suppresses post-sign-in session updates. */
43 export function connectCliSignInRedirectUrl(
44 request: ConnectAuthorizeRequest,
45 currentHref: string,
46 ): string {
47 return buildConnectCliClerkAuthorizeUrl(request) ?? currentHref;
48 }