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

providerAuthDelivery.ts

85 lines3.1 KB

1import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
2import { readCodexAuthDelivery } from "@t3tools/shared/codexAuthHandoff";
3import { Atom } from "effect/reactivity";
4import { msg } from "gt-react";
5​
6import { toastManager } from "./components/ui/toast";
7import { m } from "./i18n/messages";
8import { appAtomRegistry } from "./rpc/atomRegistry";
9import { environmentPresentations, environmentUnreachableAtom } from "./state/presentation";
10import { serverEnvironment } from "./state/server";
11​
12type ProviderAuthDelivery = NonNullable<ReturnType<typeof readCodexAuthDelivery>>;
13​
14/**
15 * Remove the code before the router, tracing, or welcome screen sees the incoming address,
16 * then hand it to its environment once that environment connects, or report that it never
17 * will. Hosted web receives only the one-time code; the selected environment verifies and
18 * stores tokens.
19 */
20export function startProviderAuthDelivery(): void {
21 if (!window.location.hash.startsWith("#codex-auth=")) return;
22 const delivery = readCodexAuthDelivery(window.location.href);
23 window.history.replaceState(
24 window.history.state,
25 "",
26 delivery?.returnUrl ?? `${window.location.pathname}${window.location.search}`,
27 );
28 if (!delivery) return;
29​
30 const presentationAtom = environmentPresentations.presentationAtom(delivery.environmentId);
31 const unreachableAtom = environmentUnreachableAtom(delivery.environmentId);
32 const destinationAtom = Atom.make((get) => {
33 if (get(presentationAtom)?.connection.phase === "connected") return "connected";
34 return get(unreachableAtom) ? "unavailable" : "pending";
35 });
36 const deliver = (destination: "connected" | "unavailable" | "pending"): boolean => {
37 if (destination === "pending") return false;
38 if (destination === "connected") void completeProviderAuth(delivery);
39 else reportUnavailableEnvironment();
40 return true;
41 };
42 if (deliver(appAtomRegistry.get(destinationAtom))) return;
43 const unsubscribe = appAtomRegistry.subscribe(destinationAtom, (destination) => {
44 if (deliver(destination)) unsubscribe();
45 });
47​
48function reportUnavailableEnvironment(): void {
49 toastManager.add({
50 type: "error",
51 title: m(msg("ChatGPT sign-in couldn't finish")),
52 description: m(msg("The environment it was for isn't available on this device.")),
53 });
55​
56async function completeProviderAuth(delivery: ProviderAuthDelivery): Promise<void> {
57 try {
58 const result = await runAtomCommand(
59 appAtomRegistry,
60 serverEnvironment.completeProviderAuth,
61 {
62 environmentId: delivery.environmentId,
63 input: {
64 instanceId: delivery.instanceId,
65 flowId: delivery.flowId,
66 callbackUrl: delivery.callbackUrl,
67 },
68 },
69 { reportFailure: false },
70 );
71 if (result._tag === "Failure") {
72 toastManager.add({
73 type: "error",
74 title: m(msg("ChatGPT sign-in couldn't finish")),
75 description: m(msg("Return to the provider and try again.")),
76 });
77 }
78 } catch {
79 toastManager.add({
80 type: "error",
81 title: m(msg("ChatGPT sign-in couldn't finish")),
82 description: m(msg("Reconnect to the environment and try again.")),
83 });
84 }