File size: 5,264 Bytes
a070805
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React from "react";
import {
  startDeviceFlow,
  pollForToken,
  DeviceFlowError,
  type DeviceAuthorizationResponse,
} from "#/api/device-flow-client";
import {
  trackCloudDeviceAuthorizationStarted,
  trackCloudDeviceAuthorizationSucceeded,
  type CloudConnectionSource,
} from "#/services/cloud-funnel-analytics";

export type DeviceFlowStatus =
  | "idle"
  | "starting"
  | "awaiting_authorization"
  | "success"
  | "error";

export interface DeviceFlowState {
  status: DeviceFlowStatus;
  /** The verification URL to show/open for the user */
  verificationUrl: string | null;
  /** User code to display as fallback */
  userCode: string | null;
  /** The resulting API key on success */
  apiKey: string | null;
  /** Error message if status is "error" */
  error: string | null;
  /** Error code for programmatic handling */
  errorCode: string | null;
}

export interface UseDeviceFlowReturn extends DeviceFlowState {
  /** Start the device flow authentication */
  start: StartDeviceFlow;
  /** Cancel an in-progress flow */
  cancel: () => void;
  /** Reset state back to idle */
  reset: () => void;
}

type StartDeviceFlow = (
  host: string,
  analyticsSource?: CloudConnectionSource,
) => void;

const initialState: DeviceFlowState = {
  status: "idle",
  verificationUrl: null,
  userCode: null,
  apiKey: null,
  error: null,
  errorCode: null,
};

/**
 * React hook for managing OAuth 2.0 Device Flow authentication.
 *
 * Usage:
 * ```tsx
 * const { status, verificationUrl, apiKey, error, start, cancel, reset } = useDeviceFlow();
 *
 * // Start auth
 * start("https://app.all-hands.dev");
 *
 * // Open browser when awaiting
 * if (status === "awaiting_authorization" && verificationUrl) {
 *   window.open(verificationUrl, "_blank");
 * }
 *
 * // Use API key on success
 * if (status === "success" && apiKey) {
 *   setApiKeyField(apiKey);
 * }
 * ```
 */
export function useDeviceFlow(): UseDeviceFlowReturn {
  const [state, setState] = React.useState<DeviceFlowState>(initialState);
  const abortControllerRef = React.useRef<AbortController | null>(null);

  const start: StartDeviceFlow = React.useCallback((host, analyticsSource) => {
    // Cancel any existing flow
    abortControllerRef.current?.abort();
    const abortController = new AbortController();
    abortControllerRef.current = abortController;

    setState({
      ...initialState,
      status: "starting",
    });

    (async () => {
      let authResponse: DeviceAuthorizationResponse;

      try {
        authResponse = await startDeviceFlow(host);
      } catch (error) {
        if (abortController.signal.aborted) return;

        const message =
          error instanceof Error
            ? error.message
            : "Failed to start device flow";
        const code = error instanceof DeviceFlowError ? error.code : undefined;

        setState({
          ...initialState,
          status: "error",
          error: message,
          errorCode: code ?? null,
        });
        return;
      }

      if (abortController.signal.aborted) return;

      trackCloudDeviceAuthorizationStarted(host, analyticsSource);

      setState({
        ...initialState,
        status: "awaiting_authorization",
        verificationUrl: authResponse.verification_uri_complete,
        userCode: authResponse.user_code,
      });

      try {
        const tokenResponse = await pollForToken(
          host,
          authResponse.device_code,
          {
            interval: authResponse.interval,
            signal: abortController.signal,
          },
        );

        if (abortController.signal.aborted) return;

        trackCloudDeviceAuthorizationSucceeded(host, analyticsSource);

        setState({
          ...initialState,
          status: "success",
          apiKey: tokenResponse.access_token,
        });
      } catch (error) {
        // Early return if component unmounted or user cancelled
        if (abortController.signal.aborted) return;

        // Defensive: handle cancellation errors that may slip through
        // (currently pollForToken only throws "cancelled" when signal.aborted,
        // which is caught above, but this guards against future changes)
        const isCancel =
          error instanceof DeviceFlowError && error.code === "cancelled";
        if (isCancel) {
          setState(initialState);
          return;
        }

        const message =
          error instanceof Error ? error.message : "Authorization failed";
        const code = error instanceof DeviceFlowError ? error.code : undefined;

        setState({
          ...initialState,
          status: "error",
          error: message,
          errorCode: code ?? null,
        });
      }
    })();
  }, []);

  const cancel = React.useCallback(() => {
    abortControllerRef.current?.abort();
    abortControllerRef.current = null;
    setState(initialState);
  }, []);

  const reset = React.useCallback(() => {
    abortControllerRef.current?.abort();
    abortControllerRef.current = null;
    setState(initialState);
  }, []);

  // Cleanup on unmount
  React.useEffect(() => {
    return () => {
      abortControllerRef.current?.abort();
    };
  }, []);

  return {
    ...state,
    start,
    cancel,
    reset,
  };
}