Advertisement
caLLowCreation

Auth Window Popup

Jul 3rd, 2020
2,572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.71 KB | None | 0 0
  1.     $("#auth-button").click((e) => {
  2.         e.preventDefault();
  3.  
  4.         showLoading();
  5.  
  6.         const obj = {
  7.             headers: { 'Authorization': 'Bearer ' + authorized.token },
  8.             type: 'GET',
  9.             url: `${urls[index]}authurl`,
  10.             success: function (authUrl) {
  11.                 function messageCallback(msg) {
  12.                     //console.log("Got new window message: ", msg.data);
  13.                     if (msg.data.hasOwnProperty('success')) {
  14.                         if (msg.data.success === true) {
  15.                             $("#loggedin-response").show();
  16.                             $("#loggedin-response").html(msg.data.message);
  17.                             $('#authorized-components-container').show();
  18.                             $('#unauthorized-components-container').hide();
  19.  
  20.                             $('#playlist-how-display-container').show();
  21.                         } else {
  22.                             $("#auth-response-message").html(msg.data.message);
  23.                             $("#auth-response-message").fadeIn(1000, () => {
  24.                                 setTimeout(() => $("#auth-response-message").fadeOut(1000), 3000);
  25.                             });
  26.                         }
  27.                         authClosed = true;
  28.                         authWindow.close();
  29.  
  30.                         hideLoading();
  31.  
  32.                         window.removeEventListener('message', messageCallback);
  33.                     }
  34.                 }
  35.  
  36.                 $("#loggedin-response").hide();
  37.                 $("#auth-response-message").hide();
  38.                 authClosed = false;
  39.  
  40.                 const authWindow = window.open(authUrl, "_blank", "width=500,height=650");
  41.  
  42.  
  43.                 const authWindowInterval = setInterval(() => {
  44.                     if (authWindow.closed) {
  45.                         clearInterval(authWindowInterval);
  46.                         if (!authClosed) {
  47.                             authClosed = true;
  48.                             hideLoading();
  49.                             $("#auth-response-message").html("Auth Canceled");
  50.                             $("#auth-response-message").fadeIn(1000, () => {
  51.                                 setTimeout(() => $("#auth-response-message").fadeOut(1000), 3000);
  52.                             });
  53.                         }
  54.                     }
  55.                 }, 100);
  56.  
  57.                 window.removeEventListener('message', messageCallback);
  58.                 window.addEventListener('message', messageCallback);
  59.             },
  60.             error: (_, error, status) => {
  61.                 logError(_, error, status);
  62.                 hideLoading();
  63.             }
  64.         };
  65.         $.ajax(obj);
  66.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement