dify/web/hooks/use-oauth.ts

37 lines
884 B
TypeScript
Raw Normal View History

2025-06-19 16:09:05 +08:00
'use client'
import { useEffect } from 'react'
export const useOAuthCallback = () => {
useEffect(() => {
2025-06-23 14:05:29 +08:00
if (window.opener) {
2025-06-19 16:09:05 +08:00
window.opener.postMessage({
type: 'oauth_callback',
}, '*')
window.close()
}
2025-06-23 14:05:29 +08:00
}, [])
2025-06-19 16:09:05 +08:00
}
2025-06-23 14:05:29 +08:00
export const openOAuthPopup = (url: string, callback: () => void) => {
2025-06-19 16:09:05 +08:00
const width = 600
const height = 600
const left = window.screenX + (window.outerWidth - width) / 2
const top = window.screenY + (window.outerHeight - height) / 2
const popup = window.open(
url,
'OAuth',
`width=${width},height=${height},left=${left},top=${top},scrollbars=yes`,
)
const handleMessage = (event: MessageEvent) => {
if (event.data?.type === 'oauth_callback') {
window.removeEventListener('message', handleMessage)
2025-06-23 14:05:29 +08:00
callback()
2025-06-19 16:09:05 +08:00
}
}
window.addEventListener('message', handleMessage)
return popup
}