support re-authorization

pull/22091/head
JzoNg 11 months ago
parent 2b0193dd79
commit d2ac384458

@ -25,7 +25,6 @@ import {
useInvalidateMCPTools, useInvalidateMCPTools,
useMCPTools, useMCPTools,
useUpdateMCP, useUpdateMCP,
useUpdateMCPAuthorizationToken,
useUpdateMCPTools, useUpdateMCPTools,
} from '@/service/use-tools' } from '@/service/use-tools'
import { openOAuthPopup } from '@/hooks/use-oauth' import { openOAuthPopup } from '@/hooks/use-oauth'
@ -53,7 +52,6 @@ const MCPDetailContent: FC<Props> = ({
const invalidateMCPTools = useInvalidateMCPTools() const invalidateMCPTools = useInvalidateMCPTools()
const { mutateAsync: updateTools, isPending: isUpdating } = useUpdateMCPTools() const { mutateAsync: updateTools, isPending: isUpdating } = useUpdateMCPTools()
const { mutateAsync: authorizeMcp, isPending: isAuthorizing } = useAuthorizeMCP() const { mutateAsync: authorizeMcp, isPending: isAuthorizing } = useAuthorizeMCP()
const { mutateAsync: updateMCPAuthorizationToken } = useUpdateMCPAuthorizationToken()
const toolList = data?.tools || [] const toolList = data?.tools || []
const handleUpdateTools = useCallback(async () => { const handleUpdateTools = useCallback(async () => {
@ -62,7 +60,7 @@ const MCPDetailContent: FC<Props> = ({
await updateTools(detail.id) await updateTools(detail.id)
invalidateMCPTools(detail.id) invalidateMCPTools(detail.id)
onUpdate() onUpdate()
}, [detail, updateTools]) }, [detail, invalidateMCPTools, onUpdate, updateTools])
const { mutate: updateMCP } = useUpdateMCP({ const { mutate: updateMCP } = useUpdateMCP({
onSuccess: onUpdate, onSuccess: onUpdate,
@ -86,17 +84,13 @@ const MCPDetailContent: FC<Props> = ({
setFalse: hideDeleting, setFalse: hideDeleting,
}] = useBoolean(false) }] = useBoolean(false)
const handleOAuthCallback = async (state: string, code: string) => { const handleOAuthCallback = useCallback((state: string) => {
if (!isCurrentWorkspaceManager) if (!isCurrentWorkspaceManager)
return return
if (detail.id !== state) if (detail.id !== state)
return return
await updateMCPAuthorizationToken({
provider_id: state,
authorization_code: code,
})
handleUpdateTools() handleUpdateTools()
} }, [detail.id, handleUpdateTools, isCurrentWorkspaceManager])
const handleAuthorize = useCallback(async () => { const handleAuthorize = useCallback(async () => {
onFirstCreate() onFirstCreate()
@ -112,7 +106,7 @@ const MCPDetailContent: FC<Props> = ({
else if (res.authorization_url) else if (res.authorization_url)
openOAuthPopup(res.authorization_url, handleOAuthCallback) openOAuthPopup(res.authorization_url, handleOAuthCallback)
}, [detail, updateMCP, hideUpdateModal, onUpdate]) }, [onFirstCreate, isCurrentWorkspaceManager, detail, authorizeMcp, handleUpdateTools, handleOAuthCallback])
const handleUpdate = useCallback(async (data: any) => { const handleUpdate = useCallback(async (data: any) => {
if (!detail) if (!detail)
@ -137,11 +131,12 @@ const MCPDetailContent: FC<Props> = ({
hideDeleteConfirm() hideDeleteConfirm()
onUpdate(true) onUpdate(true)
} }
}, [detail, showDeleting, hideDeleting, hideDeleteConfirm, onUpdate]) }, [detail, showDeleting, deleteMCP, hideDeleting, hideDeleteConfirm, onUpdate])
useEffect(() => { useEffect(() => {
if (isCreation) if (isCreation)
handleAuthorize() handleAuthorize()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []) }, [])
if (!detail) if (!detail)
@ -175,6 +170,7 @@ const MCPDetailContent: FC<Props> = ({
<Button <Button
variant='secondary' variant='secondary'
className='w-full' className='w-full'
onClick={handleAuthorize}
disabled={!isCurrentWorkspaceManager} disabled={!isCurrentWorkspaceManager}
> >
<Indicator className='mr-2' color={'green'} /> <Indicator className='mr-2' color={'green'} />

@ -6,15 +6,13 @@ export const useOAuthCallback = () => {
const searchParams = useSearchParams() const searchParams = useSearchParams()
useEffect(() => { useEffect(() => {
const code = searchParams.get('code') const MCPProviderID = searchParams.get('mcp_provider_id')
const state = searchParams.get('state')
if (code && state && window.opener) { if (MCPProviderID && window.opener) {
window.opener.postMessage({ window.opener.postMessage({
type: 'oauth_callback', type: 'oauth_callback',
payload: { payload: {
code, state: MCPProviderID,
state,
}, },
}, '*') }, '*')
window.close() window.close()
@ -22,7 +20,7 @@ export const useOAuthCallback = () => {
}, [searchParams]) }, [searchParams])
} }
export const openOAuthPopup = (url: string, callback: (state: string, code: string) => void) => { export const openOAuthPopup = (url: string, callback: (state: string) => void) => {
const width = 600 const width = 600
const height = 600 const height = 600
const left = window.screenX + (window.outerWidth - width) / 2 const left = window.screenX + (window.outerWidth - width) / 2
@ -37,8 +35,8 @@ export const openOAuthPopup = (url: string, callback: (state: string, code: stri
const handleMessage = (event: MessageEvent) => { const handleMessage = (event: MessageEvent) => {
if (event.data?.type === 'oauth_callback') { if (event.data?.type === 'oauth_callback') {
window.removeEventListener('message', handleMessage) window.removeEventListener('message', handleMessage)
const { code, state } = event.data.payload const { state } = event.data.payload
callback(state, code) callback(state)
} }
} }

Loading…
Cancel
Save