Improve OAuth polling

This commit is contained in:
JonnyWong16 2018-09-30 21:05:12 -07:00
parent dbc53ca710
commit a94207691f

View file

@ -611,7 +611,6 @@ function PlexOAuth(success, error, pre) {
if (typeof pre === "function") { if (typeof pre === "function") {
pre() pre()
} }
clearTimeout(polling);
closePlexOAuthWindow(); closePlexOAuthWindow();
plex_oauth_window = PopupCenter('', 'Plex-OAuth', 600, 700); plex_oauth_window = PopupCenter('', 'Plex-OAuth', 600, 700);
$(plex_oauth_window.document.body).html(plex_oauth_loader); $(plex_oauth_window.document.body).html(plex_oauth_loader);
@ -621,37 +620,36 @@ function PlexOAuth(success, error, pre) {
const code = data.code; const code = data.code;
plex_oauth_window.location = 'https://app.plex.tv/auth/#!?clientID=' + x_plex_headers['X-Plex-Client-Identifier'] + '&code=' + code; plex_oauth_window.location = 'https://app.plex.tv/auth/#!?clientID=' + x_plex_headers['X-Plex-Client-Identifier'] + '&code=' + code;
polling = pin;
(function poll() { (function poll() {
polling = setTimeout(function () { $.ajax({
$.ajax({ url: 'https://plex.tv/api/v2/pins/' + pin,
url: 'https://plex.tv/api/v2/pins/' + pin, type: 'GET',
type: 'GET', headers: x_plex_headers,
headers: x_plex_headers, success: function (data) {
success: function (data) { if (data.authToken){
if (data.authToken){ closePlexOAuthWindow();
closePlexOAuthWindow(); if (typeof success === "function") {
if (typeof success === "function") { success(data.authToken)
success(data.authToken)
}
} }
}, }
error: function () { },
error: function (jqXHR, textStatus, errorThrown) {
if (textStatus !== "timeout") {
closePlexOAuthWindow(); closePlexOAuthWindow();
if (typeof error === "function") { if (typeof error === "function") {
error() error()
} }
}, }
complete: function () { },
if (!plex_oauth_window.closed){ complete: function () {
poll(); if (!plex_oauth_window.closed && polling === pin){
} else { setTimeout(function() {poll()}, 1000);
clearTimeout(polling); }
} },
}, timeout: 10000
timeout: 1000 });
});
}, 1000);
})(); })();
}, function () { }, function () {
closePlexOAuthWindow(); closePlexOAuthWindow();