Guest User

Untitled

a guest
Jul 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. /* global window */
  2.  
  3. const popup = (url) => {
  4. const windowArea = {
  5. width: Math.floor(window.outerWidth * 0.8),
  6. height: Math.floor(window.outerHeight * 0.5),
  7. };
  8.  
  9. if (windowArea.width < 1000) { windowArea.width = 1000; }
  10. if (windowArea.height < 630) { windowArea.height = 630; }
  11. windowArea.left = Math.floor(window.screenX + ((window.outerWidth - windowArea.width) / 2));
  12. windowArea.top = Math.floor(window.screenY + ((window.outerHeight - windowArea.height) / 8));
  13.  
  14. const sep = (url.indexOf('?') !== -1) ? '&' : '?';
  15. const url = `${url}${sep}`;
  16. const windowOpts = `toolbar=0,scrollbars=1,status=1,resizable=1,location=1,menuBar=0,
  17. width=${windowArea.width},height=${windowArea.height},
  18. left=${windowArea.left},top=${windowArea.top}`;
  19.  
  20. const authWindow = window.open(url, 'producthuntPopup', windowOpts);
  21. // Create IE + others compatible event handler
  22. const eventMethod = window.addEventListener ? 'addEventListener' : 'attachEvent';
  23. const eventer = window[eventMethod];
  24. const messageEvent = eventMethod === 'attachEvent' ? 'onmessage' : 'message';
  25.  
  26. // Listen to message from child window
  27. const authPromise = new Promise((resolve, reject) => {
  28. eventer(messageEvent, (e) => {
  29. if (e.origin !== window.SITE_DOMAIN) {
  30. authWindow.close();
  31. reject('Not allowed');
  32. }
  33.  
  34. if (e.data.auth) {
  35. resolve(JSON.parse(e.data.auth));
  36. authWindow.close();
  37. } else {
  38. authWindow.close();
  39. reject('Unauthorised');
  40. }
  41. }, false);
  42. });
  43.  
  44. return authPromise;
  45. };
  46.  
  47. export default popup;
  48.  
  49. // On Server view after response
  50. window.opener.postMessage(
  51. { auth: { token: access_token } },
  52. window.opener.location
  53. );
  54.  
  55. window.opener.postMessage(
  56. { error: 'Login failed' },
  57. window.opener.location
  58. );
Add Comment
Please, Sign In to add comment