methods/popup-oauth-link.js

  1. import createPopup from './../lib/create-popup'
  2. /**
  3. * @method
  4. * @name EcomPassport#popupOauthLink
  5. * @description Open a new popup to received URL for OAuth and
  6. * try to fetch profile on callback or popup closed.
  7. *
  8. * @param {string} url - Popup URL (OAuth provider link)
  9. *
  10. * @returns {window|null}
  11. *
  12. * @example
  13. ecomPassport.popupOauthLink(facebookOauthLink)
  14. */
  15. export default ({ fetchOauthProfile }, emitter, [url]) => {
  16. let popupWatch = null
  17. const getCustomerInfo = fromCallback => {
  18. clearInterval(popupWatch)
  19. fetchOauthProfile()
  20. }
  21. window.passportCallback = function () {
  22. getCustomerInfo(true)
  23. }
  24. const popup = createPopup(url)
  25. if (popup) {
  26. if (typeof window === 'object' && window.focus) {
  27. popup.focus()
  28. }
  29. popupWatch = setInterval(() => {
  30. if (popup.closed) {
  31. getCustomerInfo(false)
  32. }
  33. }, 400)
  34. }
  35. return popup
  36. }