diff --git a/dashboard/src/locales/en.json b/dashboard/src/locales/en.json index a68beaa..0d2eb03 100644 --- a/dashboard/src/locales/en.json +++ b/dashboard/src/locales/en.json @@ -2898,6 +2898,7 @@ "oauthSuccess": "Authorized — save the connection", "oauthFailed": "Failed to fetch OAuth result", "oauthStartFailed": "Could not start OAuth", + "oauthPopupBlocked": "The authorization window was blocked. Allow pop-ups for this site and try again.", "oauthCompleteHint": "Enter a display name and save", "chatPicker": "Connectors", "chatPickerSearch": "Search connectors", diff --git a/dashboard/src/locales/zh.json b/dashboard/src/locales/zh.json index d3c88b2..f2648f7 100644 --- a/dashboard/src/locales/zh.json +++ b/dashboard/src/locales/zh.json @@ -3062,6 +3062,7 @@ "oauthSuccess": "授权成功,请保存连接", "oauthFailed": "获取授权结果失败", "oauthStartFailed": "无法启动 OAuth", + "oauthPopupBlocked": "授权窗口被浏览器拦截,请允许本站弹出窗口后重试", "oauthCompleteHint": "请填写显示名称并保存连接", "chatPicker": "连接器", "chatPickerSearch": "搜索连接器", diff --git a/dashboard/src/pages/Agent/Connectors/index.tsx b/dashboard/src/pages/Agent/Connectors/index.tsx index 460298e..40f9fe0 100644 --- a/dashboard/src/pages/Agent/Connectors/index.tsx +++ b/dashboard/src/pages/Agent/Connectors/index.tsx @@ -194,6 +194,7 @@ function ConnectorConfigDrawer({ const [form] = Form.useForm(); const [saving, setSaving] = useState(false); const [probing, setProbing] = useState(false); + const [authorizing, setAuthorizing] = useState(false); const [showManual, setShowManual] = useState(false); const [authInfo, setAuthInfo] = useState(null); const [instanceDetail, setInstanceDetail] = @@ -313,22 +314,30 @@ function ConnectorConfigDrawer({ }; const handleOAuth = async () => { - if (!entry) return; + if (!entry || authorizing) return; + const popup = window.open("", "octop-oauth", "width=520,height=720"); + if (!popup) { + message.error( + t( + "connectors.oauthPopupBlocked", + "授权窗口被浏览器拦截,请允许本站弹出窗口后重试", + ), + ); + return; + } + + setAuthorizing(true); try { const { authorize_url, state_id } = await connectorsApi.oauthStart( entry.kind, "/connectors", ); - const popup = window.open( - authorize_url, - "octop-oauth", - "width=520,height=720", - ); const onMessage = async (ev: MessageEvent) => { + if (ev.origin !== window.location.origin) return; if (ev.data?.type !== "octop:connector-oauth") return; if (ev.data.state_id !== state_id) return; window.removeEventListener("message", onMessage); - popup?.close(); + popup.close(); try { const pending = await connectorsApi.oauthPending(state_id); const tokens = pending.tokens ?? {}; @@ -347,11 +356,15 @@ function ConnectorConfigDrawer({ } }; window.addEventListener("message", onMessage); + popup.location.replace(authorize_url); } catch (e) { + popup.close(); console.error(e); message.error( apiErrorMessage(e, t("connectors.oauthStartFailed", "无法启动 OAuth")), ); + } finally { + setAuthorizing(false); } }; @@ -520,6 +533,7 @@ function ConnectorConfigDrawer({