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

t3-connect.md

273 lines13.4 KB

1# T3 Connect
2​
3> For maintainers. Using T3 Code? See [docs/user](../user/).
4​
5T3 Connect uses one Clerk application for web, desktop, and mobile authentication. The relay verifies
6two kinds of bearer credential: template JWTs generated from the `t3-relay` template with the shared
7`t3-code-relay` audience, and Clerk OAuth tokens issued to the CLI. `verifyRelayClientBearerToken` in
8`infra/relay/src/http/Api.ts` tries the template/session path first and falls back to OAuth
9verification (`acceptsToken: "oauth_token"`), so the CLI's OAuth credential works without a JWT
10template.
11​
12For the wider system diagram, see
13[t3-code-connect-auth-flow.html](./t3-code-connect-auth-flow.html).
14​
15## Application Keys
16​
17T3 Connect is disabled in a fresh clone. To enable it for source builds against the production
18deployment, copy the repository-root example file:
19​
20```sh
21cp .env.example .env
22```
23​
24`.env.example` carries the production public identifiers (the same values baked into official
25release builds). To target a different Clerk application or relay, set the values yourself in a
26repository-root `.env` or `.env.local` file:
27​
28```dotenv
29T3CODE_CLERK_PUBLISHABLE_KEY=<publishable key>
30T3CODE_CLERK_JWT_TEMPLATE=<JWT template name>
31T3CODE_CLERK_CLI_OAUTH_CLIENT_ID=<public OAuth application client ID>
32T3CODE_RELAY_URL=https://relay.example.com
33```
34​
35The shared client loader projects these canonical values into framework-specific `VITE_*` and
36`EXPO_PUBLIC_*` aliases. Existing aliases remain accepted as overrides for compatibility, but new
37client configuration should use the canonical names.
38​
39Configuration precedence is:
40​
411. Process or CI environment variables.
422. Repository-root `.env.local`.
433. Repository-root `.env`.
44​
45The Clerk publishable key, JWT template name, CLI OAuth client ID, and relay URL are public
46identifiers, not secrets.
47Web, desktop, mobile, and bundled server builds statically inject the values they consume during
48their build step. A built artifact does not need an environment file at runtime. CI release builds
49should set `T3CODE_CLERK_PUBLISHABLE_KEY`, `T3CODE_CLERK_JWT_TEMPLATE`,
50`T3CODE_CLERK_CLI_OAUTH_CLIENT_ID`, and `T3CODE_RELAY_URL` before building. EAS preview and
51production builds only need the Clerk publishable key, JWT template name, and relay URL in their EAS
52environment.
53​
54When any client-facing public value is absent, cloud UI is omitted. The `t3 connect` command group is
55always registered: when the CLI public values are absent, `makeCli` in `apps/server/src/bin.ts`
56registers a hidden fallback `connect` command that reports the missing configuration instead of
57silently vanishing from help. The bundled server still accepts runtime overrides for self-hosted or
58operator-managed deployments.
59​
60For a hosted relay deployment, copy `infra/relay/.env.example` to `infra/relay/.env`. The relay
61deployment reads `RELAY_DOMAIN`, `RELAY_API_ZONE_NAME`, `RELAY_TUNNEL_ZONE_NAME`,
62`CLERK_PUBLISHABLE_KEY`, and `CLERK_JWT_AUDIENCE` through Effect `Config`. There are no checked-in
63deployment defaults.
64`vp run --filter t3code-relay deploy` invokes Alchemy from the relay directory, so Alchemy loads
65`infra/relay/.env`. After a successful deployment, the wrapper updates the repository-root `.env`
66with the deployed HTTPS relay URL. The relay still requires
67`CLERK_SECRET_KEY` as an Alchemy secret. Never put `CLERK_SECRET_KEY` in a client application
68environment or commit it to the repository.
69​
70The `prod` Alchemy stage owns the retained PlanetScale database. Non-production stages reference
71that database and provision isolated PlanetScale branches, so deploy `prod` before creating a
72personal developer stage.
73​
74## Headless CLI OAuth Application
75​
76The `t3 connect` commands authorize a headless environment with a separate Clerk OAuth application.
77This uses an OAuth public client with PKCE, so the CLI stores no client secret.
78​
79In **Clerk Dashboard > OAuth applications**:
80​
811. Create an OAuth application for the T3 CLI.
822. Enable the **Public** option so authorization-code exchange uses PKCE.
833. Add **both** allowed redirect URIs:
84 - `http://127.0.0.1:34338/callback` for the loopback listener;
85 - `https://app.t3.codes/connect/callback` for the hosted out-of-band flow. This is
86 `connectCallbackUrl(DEFAULT_HOSTED_APP_URL)` from `packages/shared/src/connectAuth.ts`, so a
87 custom `T3CODE_HOSTED_APP_URL` means `$T3CODE_HOSTED_APP_URL/connect/callback` instead.
88 Omitting it breaks headless and SSH authorization.
894. Enable the `openid`, `profile`, and `email` scopes.
905. Set `T3CODE_CLERK_CLI_OAUTH_CLIENT_ID` in the repository-root `.env` file and release build
91 environment to the generated public client ID.
92​
93Both CLI flows start at the hosted `/connect` page (`buildConnectAuthorizeRequestUrl` in
94`packages/shared/src/connectAuth.ts`), which waits for a Clerk session and then forwards the request
95to Clerk's `/oauth/authorize`. The CLI never opens `/oauth/authorize` directly: a signed-out browser
96sent there goes through Clerk's sign-in redirect, which drops the authorize query parameters and
97fails the flow with `unsupported_response_type` or an empty `state` (#5051). The loopback flow marks
98the request with a `port` fragment parameter so the hosted page asks Clerk to redirect the
99authorization code straight to `http://127.0.0.1:<port>/callback`; the out-of-band flow omits it and
100uses the hosted `/connect/callback` page instead. The CLI derives Clerk's frontend API URL from the
101publishable key and calls only the `/oauth/token` endpoint directly. The relay is not involved in
102the OAuth handshake; it only validates the issued Clerk bearer token when the CLI manages an
103environment link.
104​
105The connect command group is:
106​
107```sh
108t3 connect # default: onboarding
109t3 connect login
110t3 connect link # --publish-only
111t3 connect status # --json
112t3 connect publish # --disable
113t3 connect unlink
114t3 connect logout
115```
116​
117`t3 serve` is a separate top-level command, not a connect subcommand.
118​
119`t3 connect login` opens the Clerk authorization flow and stores the CLI credential without enabling
120cloud exposure. `t3 connect link` installs the pinned managed `cloudflared` binary when needed,
121authorizes when needed, and records durable intent to expose the environment. It works without a
122running T3 server. The next `t3 serve` or `t3 start` reconciles the relay link and launches the
123managed tunnel. `t3 connect unlink` records disabled intent immediately, stops a reachable running
124connector, and attempts to revoke the relay-side environment record. It retains the stored CLI
125authorization so `t3 connect link` can re-enable exposure without another browser flow. `t3 connect
126logout` performs the same cleanup and removes the stored CLI authorization.
127​
128The background service has an independent lifecycle. Connect setup may offer to install it, but
129logout leaves it running; manage it with `t3 service status`, `install`, `update`, and `uninstall`.
130​
131### Headless and SSH authorization
132​
133The loopback OAuth callback listener binds to port `34338`. That path only works when a browser on
134the same machine can reach it, so `authorizeCli` in `apps/server/src/cli/connect.ts` automatically
135selects the out-of-band flow when `--headless` is passed or when it detects SSH through
136`SSH_CONNECTION` or `SSH_TTY`. The out-of-band flow prints the hosted `/connect` authorization URL
137and accepts a pasted authorization code, so no port is involved.
138​
139Port forwarding is therefore optional, not required. Forward the port only if you specifically want
140the loopback flow over SSH:
141​
142```sh
143ssh -L 34338:127.0.0.1:34338 <host>
144```
145​
146## JWT Template
147​
148In **Clerk Dashboard > JWT templates**, create a template with:
149​
150| Setting | Value |
151| ------- | ---------------------------- |
152| Name | `t3-relay` |
153| Claims | `{ "aud": "t3-code-relay" }` |
154​
155Set `T3CODE_CLERK_JWT_TEMPLATE=t3-relay` in the repository-root `.env`, and set
156`CLERK_JWT_AUDIENCE=t3-code-relay` in `infra/relay/.env`. Define `CLERK_JWT_TEMPLATE` and
157`CLERK_JWT_AUDIENCE` in the production relay deployment environment as well. The stable `aud` value
158is shared by production and non-production relay stages. The client-facing `T3CODE_RELAY_URL` still
159selects the concrete relay deployment, but changing that URL does not require a JWT template change.
160​
161## Desktop OAuth Redirect Allowlist
162​
163The desktop app opens OAuth in the system browser and returns to the app with a custom URL scheme.
164In **Clerk Dashboard > Native applications**, enable the Native API and add these entries under the
165mobile SSO redirect allowlist:
166​
167```text
168t3code-dev://app/
169t3code://app/
170```
171​
172Local desktop development uses `t3code-dev://app`, while packaged builds use `t3code://app`. Add the
173matching origin to each Clerk instance's Backend API `allowed_origins` array as well. The development
174Clerk instance should only need `t3code-dev://app`; the production Clerk instance should only need
175`t3code://app`. `@clerk/electron` owns the native request adapter, encrypted Clerk token persistence,
176external-browser OAuth transport, and callback delivery for initial sign-in and linked-account flows.
177​
178There is currently no Dashboard UI for `allowed_origins`. Preserve any existing entries and update
179the instance through the Backend API:
180​
181```sh
182curl -X PATCH https://api.clerk.com/v1/instance \
183 -H "Content-Type: application/json" \
184 -H "Authorization: Bearer $CLERK_SECRET_KEY" \
185 -d '{"allowed_origins":["t3code://app"]}'
186```
187​
188Never put `CLERK_SECRET_KEY` in the desktop app, a client-facing environment file, or a build
189artifact.
190​
191## Desktop Passkeys
192​
193The production macOS bundle ID is `com.t3tools.t3code`. To enable native passkeys:
194​
1951. Create an explicit macOS App ID for `com.t3tools.t3code` in the Apple Developer portal and enable
196 **Associated Domains**.
1972. Create a compatible macOS provisioning profile for that App ID and the certificate used to sign
198 the distributed app.
1993. In Clerk's Native API settings, add an iOS app with the same Apple Team ID and bundle ID. This is
200 also the configuration point for Electron/macOS passkeys.
2014. Confirm Clerk serves `https://<frontend-api>/.well-known/apple-app-site-association` and that
202 `webcredentials.apps` contains `<TEAM_ID>.com.t3tools.t3code`.
2035. Set the local or CI signing configuration described below.
204​
205For a local signed build, add these values to `.env.local` or export them before invoking the
206desktop artifact command:
207​
208```dotenv
209T3CODE_APPLE_TEAM_ID=ABC1234567
210T3CODE_MACOS_PROVISIONING_PROFILE=/absolute/path/to/t3code.provisionprofile
211# Optional: comma-separated override when Clerk's RP ID differs from the Frontend API hostname.
212T3CODE_CLERK_PASSKEY_RP_DOMAINS=example.clerk.accounts.dev,clerk.example.com
213```
214​
215When `T3CODE_CLERK_PASSKEY_RP_DOMAINS` is absent, the build derives the RP domain from
216`T3CODE_CLERK_PUBLISHABLE_KEY`. Signed macOS builds fail early if the Team ID, provisioning profile,
217or RP-domain configuration is missing. The generated main-app entitlements include every configured
218`webcredentials:<domain>` entry; helper apps keep Electron's minimal default entitlements.
219​
220The normal `dev:desktop` launcher is unsigned and cannot complete macOS passkey ceremonies. For
221renderer HMR, build and install a signed app first, run the renderer dev server, then launch the
222installed app executable with `VITE_DEV_SERVER_URL` and `T3CODE_PORT` set. Rebuild the signed app
223after native dependency, main-process, preload, entitlement, provisioning, or signing changes;
224renderer-only changes can reuse the installed app.
225​
226For the default development ports, run `pnpm dev:web` in one terminal and launch the installed
227binary from another:
228​
229```sh
230VITE_DEV_SERVER_URL=http://127.0.0.1:5733 \
231T3CODE_PORT=13773 \
232 "/Applications/T3 Code (Alpha).app/Contents/MacOS/T3 Code (Alpha)"
233```
234​
235After changing Associated Domains, bump the build version before rebuilding; macOS may otherwise
236reuse stale Shared Web Credentials metadata for the same app/version pair.
237​
238Verify the installed bundle before testing:
239​
240```sh
241codesign --verify --deep --strict "/Applications/T3 Code (Alpha).app"
242codesign -d --entitlements :- "/Applications/T3 Code (Alpha).app"
243```
244​
245The current mobile UI uses Clerk's native authentication view. If a future mobile browser OAuth
246flow uses a custom redirect URI, add that exact URI to the same allowlist.
247​
248## Sign-in Surfaces
249​
250Signed-in users manage T3 Connect under **Connections**. The settings sidebar also has dedicated
251controls, rendered by `SettingsSidebarNav.tsx`: `T3ConnectSidebarSignIn` in the footer shows a
252**Sign in to T3 Connect** button while signed out, and `T3ConnectSidebarAvatar` shows a Clerk
253`UserButton` account control while signed in. Both are gated on cloud public configuration.
254Desktop renders the same web bundle, so it has them too. The waitlist enrollment flow from the
255private beta was removed when Connect went GA; sign-up is open unless a Clerk restriction below is
256enabled.
257​
258## Restricting Sign-ups: Known-User Allowlist
259​
260For a closed deployment where all permitted users are known in advance, restrict sign-up to
261permitted email addresses or domains:
262​
2631. In **Clerk Dashboard > Restrictions > Allowlist**, add each permitted email address or email
264 domain.
2652. Enable the allowlist and save.
2663. Alternatively, enable **Restricted mode** when all new users must be explicitly invited or
267 manually created.
268​
269Do not enable an empty allowlist: it blocks all new sign-ups.
270​
271Clerk allowlists control who can sign up. They do not revoke an existing user's active cloud
272access. To remove an already-created user's access, ban that user in Clerk so their active
273sessions are ended and future sign-ins are rejected.