The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Shared browser-OAuth ceremony for auth providers: an |
| 3 | * {@link Interaction.browserLogin} that waits for the provider's local |
| 4 | * callback listener, with manual code entry as the fallback. |
| 5 | * |
| 6 | * {@link deviceOAuth} is the device-code (RFC 8628) counterpart. |
| 7 | * |
| 8 | * Built-in providers (Planetscale, Cloudflare) and custom stack-provided |
| 9 | * auth providers should all route their browser flows through these so the |
| 10 | * login UX stays uniform. |
| 11 | */ |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Interaction from "../Interaction.ts"; |
| 14 | import { CallbackServerStartError, type DeviceAuthorization } from "./OAuthFlow.ts"; |
| 15 | |
| 16 | export interface BrowserOAuthOptions<A, E1, R1, E2, R2> { |
| 17 | /** Display name, e.g. "Cloudflare" — used in the prompt title. */ |
| 18 | provider: string; |
| 19 | /** The authorization URL the browser was pointed at. */ |
| 20 | url: string; |
| 21 | /** Resolves when the local callback listener receives the redirect. */ |
| 22 | callback: Effect.Effect<A, E1, R1>; |
| 23 | /** Exchanges a manually pasted code / callback URL for credentials. */ |
| 24 | exchange: (input: string) => Effect.Effect<A, E2, R2>; |
| 25 | /** Spinner label. @default "waiting for browser authorization (up to 5 minutes)…" */ |
| 26 | waitingLabel?: string; |
| 27 | } |
| 28 | |
| 29 | export const browserOAuth = Effect.fn(function* <A, E1, R1, E2, R2>( |
| 30 | options: BrowserOAuthOptions<A, E1, R1, E2, R2>, |
| 31 | ) { |
| 32 | // A callback server that cannot start (e.g. the port is taken) must not |
| 33 | // fail the race when the user can paste the code instead — warn and hang |
| 34 | // so manual entry stays available. Without terminal input there is no |
| 35 | // manual entry, so the startup error fails the login. Genuine OAuth |
| 36 | // failures delivered via the callback always fail fast. |
| 37 | const { input } = (yield* Interaction.Interaction).terminal; |
| 38 | return yield* Interaction.browserLogin({ |
| 39 | provider: options.provider, |
| 40 | url: options.url, |
| 41 | waitingLabel: options.waitingLabel, |
| 42 | waitFor: input |
| 43 | ? options.callback.pipe( |
| 44 | Effect.catchIf( |
| 45 | (e): e is E1 & CallbackServerStartError => e instanceof CallbackServerStartError, |
| 46 | (e) => |
| 47 | Interaction.warning(`${e.message} — paste the authorization code instead.`).pipe( |
| 48 | Effect.andThen(Effect.never), |
| 49 | ), |
| 50 | ), |
| 51 | ) |
| 52 | : options.callback, |
| 53 | manualCode: { |
| 54 | label: "Paste the authorization code or callback URL", |
| 55 | validate: (value) => (value.trim().length > 0 ? undefined : "Paste a code or URL"), |
| 56 | exchange: options.exchange, |
| 57 | }, |
| 58 | }); |
| 59 | }); |
| 60 | |
| 61 | export interface DeviceOAuthOptions<A, E, R> { |
| 62 | /** Display name, e.g. "Cloudflare" — used in the prompt title. */ |
| 63 | provider: string; |
| 64 | /** The pending device authorization to show the user. */ |
| 65 | authorization: DeviceAuthorization; |
| 66 | /** Polls until the user approves; usually `client.deviceToken(authorization)`. */ |
| 67 | token: Effect.Effect<A, E, R>; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Shared device-authorization (RFC 8628) ceremony: open the verification |
| 72 | * URL, show the user code, and wait for the token poll. The prompt only |
| 73 | * renders the URL/code and re-opens the browser; the poll always wins. |
| 74 | */ |
| 75 | export const deviceOAuth = <A, E, R>(options: DeviceOAuthOptions<A, E, R>) => |
| 76 | Interaction.browserLogin({ |
| 77 | provider: options.provider, |
| 78 | url: options.authorization.verificationUriComplete, |
| 79 | code: options.authorization.userCode, |
| 80 | waitFor: options.token, |
| 81 | }); |