fix(webui): complete Copilot device sign-in in the browser

Reuse the device-flow adapter from #5498, expose its prompt in the existing OAuth dialog, and keep credential persistence scoped to uncancelled completion.

Co-authored-by: chengyongru <chengyongru.ai@gmail.com>
This commit is contained in:
Xubin Renandchengyongru committed 2026-09-21 00:13:46 +08:00
1 parent aca59431fe
commit a44f28e656
24 files changed
+563 -39

No files matched your search

@@ -51,6 +51,7 @@ import { useLogoFallback } from "@/hooks/useLogoFallback";
import { useMediaQuery } from "@/hooks/useMediaQuery";
import { providerBrand } from "@/lib/provider-brand";
import { cn } from "@/lib/utils";
import { copyTextToClipboard } from "@/lib/clipboard";
import type {
NanobotFeaturesPayload,
ProviderOAuthAuthorizationRequired,
@@ -272,6 +273,8 @@ export function ProviderOAuthLoginDialog({
}) {
const { t } = useTranslation();
const expectsCallbackUrl = flow?.completion_input === "callback_url";
const isDeviceCode = flow?.completion_input === "device_code";
const [copiedCode, setCopiedCode] = useState<string | null>(null);
const inputId = expectsCallbackUrl ? "provider-oauth-callback" : "provider-oauth-code";
const inputLabel = expectsCallbackUrl
? t("settings.oauth.callbackUrl")
@@ -295,7 +298,9 @@ export function ProviderOAuthLoginDialog({
<DialogHeader>
<DialogTitle>{providerLabel}</DialogTitle>
<DialogDescription>
{expectsCallbackUrl
{isDeviceCode
? t("settings.oauth.deviceCodeHelp")
: expectsCallbackUrl
? remoteBrowserAccess
? t("settings.oauth.remoteCallbackHelp")
: t("settings.oauth.localCallbackHelp")
@@ -311,12 +316,37 @@ export function ProviderOAuthLoginDialog({
<Loader2 className="h-3.5 w-3.5 shrink-0 animate-spin" aria-hidden />
)}
<span>
{expectsCallbackUrl && remoteBrowserAccess
{isDeviceCode
? t("settings.oauth.waitingForDeviceApproval")
: expectsCallbackUrl && remoteBrowserAccess
? t("settings.oauth.pasteCallbackToContinue")
: t("settings.oauth.waitingForCallback")}
</span>
</div>
<div className="space-y-2">
{isDeviceCode ? (
<div className="space-y-2">
<label htmlFor="provider-device-code" className="block text-xs font-medium text-foreground">
{t("settings.oauth.deviceCode")}
</label>
<div className="flex items-center gap-2">
<Input
id="provider-device-code"
value={flow?.user_code ?? ""}
readOnly
onFocus={(event) => event.target.select()}
className="h-12 text-center font-mono text-xl tracking-widest"
/>
<Button type="button" variant="outline" onClick={async () => {
if (flow?.user_code && await copyTextToClipboard(flow.user_code)) {
setCopiedCode(flow.flow_id);
}
}}>
<Clipboard className="mr-2 h-4 w-4" aria-hidden />
{copiedCode === flow?.flow_id ? t("code.copied") : t("code.copy")}
</Button>
</div>
</div>
) : <div className="space-y-2">
<label
htmlFor={inputId}
className="block text-xs font-medium text-foreground"
@@ -345,7 +375,7 @@ export function ProviderOAuthLoginDialog({
spellCheck={false}
/>
)}
</div>
</div>}
{error ? (
<p
role="alert"
@@ -357,13 +387,15 @@ export function ProviderOAuthLoginDialog({
<DialogFooter>
<Button type="button" variant="outline" onClick={onOpenAuthorization}>
<ExternalLink className="mr-2 h-4 w-4" aria-hidden />
{expectsCallbackUrl
{isDeviceCode
? t("settings.oauth.openGitHub")
: expectsCallbackUrl
? t("settings.oauth.openChatGPT")
: t("settings.oauth.signIn")}
</Button>
<Button type="submit" disabled={!authorizationResponse.trim() || completing}>
{!isDeviceCode ? <Button type="submit" disabled={!authorizationResponse.trim() || completing}>
{completing ? t("settings.oauth.signingIn") : t("settings.oauth.finishSignIn")}
</Button>
</Button> : null}
</DialogFooter>
</form>
</DialogContent>
@@ -1,4 +1,4 @@
import { useCallback, type Dispatch, type SetStateAction } from "react";
import { useCallback, useEffect, useRef, type Dispatch, type SetStateAction } from "react";
import type { TFunction } from "i18next";
import type {
@@ -16,6 +16,7 @@ import type { ModelSettingsState } from "@/components/settings/models/useModelSe
import { normalizeContextWindowTokens } from "@/components/settings/shared/ModelControls";
import {
ApiError,
cancelProviderOAuth,
completeProviderOAuth,
createModelConfiguration,
createProviderSettings,
@@ -60,7 +61,7 @@ interface ModelSettingsActionsOptions {
setError: Dispatch<SetStateAction<string | null>>;
onModelNameChange: (modelName: string | null) => void;
remoteBrowserAccess: boolean;
closeProviderOAuthFlow: () => void;
closeProviderOAuthFlow: (cancelPending?: boolean) => void;
installCapabilities: (names: string[]) => Promise<boolean>;
modelDirty: boolean;
configuredModelProviderOptions: Array<{ name: string; label: string }>;
@@ -82,6 +83,11 @@ export function useModelSettingsActions({
modelDirty,
configuredModelProviderOptions,
}: ModelSettingsActionsOptions) {
const oauthMounted = useRef(true);
useEffect(() => {
oauthMounted.current = true;
return () => { oauthMounted.current = false; };
}, []);
const {
expandedProvider,
form,
@@ -492,6 +498,10 @@ export function useModelSettingsActions({
)
: await logoutProviderOAuth(client, providerName);
if (isProviderOAuthAuthorizationRequired(payload)) {
if (payload.completion_input === "device_code" && !oauthMounted.current) {
await cancelProviderOAuth(client, payload.provider, payload.flow_id).catch(() => {});
return;
}
try {
if (popup && !popup.closed) popup.location.href = payload.authorization_url;
} catch {
@@ -505,7 +515,7 @@ export function useModelSettingsActions({
return;
}
popup?.close();
closeProviderOAuthFlow();
closeProviderOAuthFlow(false);
applyPayload(payload, { preserveAgentForm: action === "login" });
setError(null);
} catch (err) {
@@ -533,7 +543,7 @@ export function useModelSettingsActions({
if (isProviderOAuthPending(payload)) return;
applyPayload(payload, { preserveAgentForm: true });
setError(null);
closeProviderOAuthFlow();
closeProviderOAuthFlow(false);
} catch (err) {
if (providerOAuthFlowRef.current?.flow_id === flow.flow_id) {
setProviderOAuthDialogError((err as Error).message);
@@ -3,7 +3,7 @@ import { useEffect, type Dispatch, type SetStateAction } from "react";
import type { ApplySettingsPayload } from "@/components/settings/contracts";
import { providerFormFromRow } from "@/components/settings/models/ProviderSettings";
import type { ModelSettingsState } from "@/components/settings/models/useModelSettingsState";
import { completeProviderOAuth } from "@/lib/api";
import { cancelProviderOAuth, completeProviderOAuth } from "@/lib/api";
import type { NanobotClient } from "@/lib/nanobot-client";
import type {
ProviderOAuthCompletionResult,
@@ -22,7 +22,7 @@ interface ProviderOAuthPollingOptions {
client: NanobotClient;
applyPayload: ApplySettingsPayload;
setError: Dispatch<SetStateAction<string | null>>;
closeProviderOAuthFlow: () => void;
closeProviderOAuthFlow: (cancelPending?: boolean) => void;
}
export function useProviderOAuthPolling({
@@ -58,7 +58,7 @@ export function useProviderOAuthPolling({
}
applyPayload(payload, { preserveAgentForm: true });
setError(null);
closeProviderOAuthFlow();
closeProviderOAuthFlow(false);
} catch (err) {
if (
cancelled
@@ -72,6 +72,12 @@ export function useProviderOAuthPolling({
return () => {
cancelled = true;
if (timer !== null) window.clearTimeout(timer);
if (providerOAuthFlow.completion_input === "device_code"
&& providerOAuthFlowRef.current?.flow_id === providerOAuthFlow.flow_id) {
providerOAuthFlowRef.current = null;
void cancelProviderOAuth(client, providerOAuthFlow.provider, providerOAuthFlow.flow_id)
.catch(() => {});
}
};
}, [applyPayload, client, closeProviderOAuthFlow, providerOAuthFlow]);
}