You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536
  1. 'use client'
  2. import { useEffect } from 'react'
  3. export const useOAuthCallback = () => {
  4. useEffect(() => {
  5. if (window.opener) {
  6. window.opener.postMessage({
  7. type: 'oauth_callback',
  8. }, '*')
  9. window.close()
  10. }
  11. }, [])
  12. }
  13. export const openOAuthPopup = (url: string, callback: () => void) => {
  14. const width = 600
  15. const height = 600
  16. const left = window.screenX + (window.outerWidth - width) / 2
  17. const top = window.screenY + (window.outerHeight - height) / 2
  18. const popup = window.open(
  19. url,
  20. 'OAuth',
  21. `width=${width},height=${height},left=${left},top=${top},scrollbars=yes`,
  22. )
  23. const handleMessage = (event: MessageEvent) => {
  24. if (event.data?.type === 'oauth_callback') {
  25. window.removeEventListener('message', handleMessage)
  26. callback()
  27. }
  28. }
  29. window.addEventListener('message', handleMessage)
  30. return popup
  31. }