Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend): detect closed OAuth popup and allow dismissing waiting modal (#12443)

Ottoab7c38bda70f
diff --git a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/CredentialsInput.tsx b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/CredentialsInput.tsx
index 53fe620f9a..6c8e061895 100644
--- a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/CredentialsInput.tsx
+++ b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/CredentialsInput.tsx
@@ -81,7 +81,7 @@ export function CredentialsInput({
isHostScopedCredentialsModalOpen,
isCredentialTypeSelectorOpen,
isOAuth2FlowInProgress,
- oAuthPopupController,
+ cancelOAuthFlow,
actionButtonText,
setAPICredentialsModalOpen,
setUserPasswordCredentialsModalOpen,
@@ -158,7 +158,7 @@ export function CredentialsInput({
{supportsOAuth2 && (
<OAuthFlowWaitingModal
open={isOAuth2FlowInProgress}
- onClose={() => oAuthPopupController?.abort("canceled")}
+ onClose={cancelOAuthFlow}
providerName={providerName}
/>
)}
diff --git a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/useCredentialsInput.ts b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/useCredentialsInput.ts
index 2e2a5d586f..0ffdbcb053 100644
--- a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/useCredentialsInput.ts
+++ b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/useCredentialsInput.ts
@@ -6,7 +6,12 @@ import {
CredentialsMetaInput,
} from "@/lib/autogpt-server-api/types";
import { postV2InitiateOauthLoginForAnMcpServer } from "@/app/api/__generated__/endpoints/mcp/mcp";
-import { openOAuthPopup } from "@/lib/oauth-popup";
+import {
+ OAUTH_ERROR_FLOW_CANCELED,
+ OAUTH_ERROR_FLOW_TIMED_OUT,
+ OAUTH_ERROR_WINDOW_CLOSED,
+ openOAuthPopup,
+} from "@/lib/oauth-popup";
import { useQueryClient } from "@tanstack/react-query";
import { useEffect, useRef, useState } from "react";
import {
@@ -49,8 +54,6 @@ export function useCredentialsInput({
const [isCredentialTypeSelectorOpen, setCredentialTypeSelectorOpen] =
useState(false);
const [isOAuth2FlowInProgress, setOAuth2FlowInProgress] = useState(false);
- const [oAuthPopupController, setOAuthPopupController] =
- useState<AbortController | null>(null);
const [oAuthError, setOAuthError] = useState<string | null>(null);
const [credentialToDelete, setCredentialToDelete] = useState<{
id: string;
@@ -212,12 +215,6 @@ export function useCredentialsInput({
});
oauthAbortRef.current = cleanup.abort;
- // Expose abort signal for the waiting modal's cancel button
- const controller = new AbortController();
- cleanup.signal.addEventListener("abort", () =>
- controller.abort("completed"),
- );
- setOAuthPopupController(controller);
const result = await promise;
@@ -252,14 +249,16 @@ export function useCredentialsInput({
provider,
});
} catch (error) {
- if (error instanceof Error && error.message === "OAuth flow timed out") {
- setOAuthError("OAuth flow timed out");
+ const message = error instanceof Error ? error.message : String(error);
+ if (
+ message === OAUTH_ERROR_WINDOW_CLOSED ||
+ message === OAUTH_ERROR_FLOW_CANCELED
+ ) {
+ // User closed the popup or clicked cancel — not an error
+ } else if (message === OAUTH_ERROR_FLOW_TIMED_OUT) {
+ setOAuthError(OAUTH_ERROR_FLOW_TIMED_OUT);
} else {
- setOAuthError(
- `OAuth error: ${
- error instanceof Error ? error.message : String(error)
- }`,
- );
+ setOAuthError(`OAuth error: ${message}`);
}
} finally {
setOAuth2FlowInProgress(false);
@@ -311,6 +310,10 @@ export function useCredentialsInput({
}
}
+ function cancelOAuthFlow() {
+ oauthAbortRef.current?.("canceled");
+ }
+
function handleDeleteCredential(credential: { id: string; title: string }) {
setCredentialToDelete(credential);
}
@@ -345,7 +348,7 @@ export function useCredentialsInput({
isHostScopedCredentialsModalOpen,
isCredentialTypeSelectorOpen,
isOAuth2FlowInProgress,
- oAuthPopupController,
+ cancelOAuthFlow,
credentialToDelete,
deleteCredentialsMutation,
actionButtonText: getActionButtonText(
diff --git a/autogpt_platform/frontend/src/lib/oauth-popup.ts b/autogpt_platform/frontend/src/lib/oauth-popup.ts
index 2927887751..235dc59e0b 100644
--- a/autogpt_platform/frontend/src/lib/oauth-popup.ts
+++ b/autogpt_platform/frontend/src/lib/oauth-popup.ts
@@ -8,6 +8,10 @@
const DEFAULT_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes
+export const OAUTH_ERROR_WINDOW_CLOSED = "Sign-in window was closed";
+export const OAUTH_ERROR_FLOW_CANCELED = "OAuth flow was canceled";
+export const OAUTH_ERROR_FLOW_TIMED_OUT = "OAuth flow timed out";
+
export type OAuthPopupResult = {
code: string;
state: string;
@@ -156,11 +160,34 @@ export function openOAuthPopup(
);
}
+ // Detect popup closed by user (without completing sign-in)
+ if (popup) {
+ const closedPollInterval = setInterval(() => {
+ if (popup.closed && !handled) {
+ clearInterval(closedPollInterval);
+ handled = true;
+ reject(new Error(OAUTH_ERROR_WINDOW_CLOSED));
+ controller.abort("popup_closed");
+ }
+ }, 500);
+ controller.signal.addEventListener("abort", () =>
+ clearInterval(closedPollInterval),
+ );
+ }
+
+ // Reject on abort (e.g. from cancel button in the waiting modal)
+ controller.signal.addEventListener("abort", () => {
+ if (!handled) {
+ handled = true;
+ reject(new Error(OAUTH_ERROR_FLOW_CANCELED));
+ }
+ });
+
// Timeout
const timeoutId = setTimeout(() => {
if (!handled) {
handled = true;
- reject(new Error("OAuth flow timed out"));
+ reject(new Error(OAUTH_ERROR_FLOW_TIMED_OUT));
controller.abort("timeout");
}
}, timeout);