Refactor Plex OAuth code

This commit is contained in:
JonnyWong16 2018-07-05 09:06:03 -07:00
commit c0b960bccf
5 changed files with 181 additions and 237 deletions

View file

@ -212,6 +212,7 @@
<script src="${http_root}js/jquery-2.1.4.min.js"></script>
<script src="${http_root}js/bootstrap.min.js"></script>
<script src="${http_root}js/selectize.min.js"></script>
<script src="${http_root}js/platform.min.js"></script>
<script src="${http_root}js/script.js${cache_param}"></script>
<script src="${http_root}js/bootstrap-wizard.min.js"></script>
<script>
@ -467,87 +468,24 @@ $(document).ready(function() {
$("#pms-verify-status").html("");
});
const x_plex_headers = {
'Accept': 'application/json',
'X-Plex-Product': '${plexpy.common.PRODUCT}',
'X-Plex-Version': '${plexpy.common.RELEASE}',
'X-Plex-Client-Identifier': '${plexpy.CONFIG.PMS_UUID}',
'X-Plex-Platform': '${plexpy.common.PLATFORM}',
'X-Plex-Platform-Version': '${plexpy.common.PLATFORM_RELEASE}',
'X-Plex-Device': '${plexpy.common.PLATFORM} ${plexpy.common.PLATFORM_RELEASE}',
'X-Plex-Device-Name': '${plexpy.common.PLATFORM_DEVICE_NAME}'
};
getPlexOAuthPin = function () {
var deferred = $.Deferred();
$.ajax({
url: 'https://plex.tv/api/v2/pins?strong=true',
type: 'POST',
headers: x_plex_headers,
success: function(data) {
deferred.resolve({pin: data.id, code: data.code});
},
error: function() {
deferred.reject();
}
});
return deferred;
};
var polling = null;
$('#sign-in-plex').click(function() {
function OAuthPreFunction() {
$("#pms_token").val('');
$("#pms-token-status").html('<i class="fa fa-refresh fa-spin"></i>&nbsp; Waiting for authentication...').fadeIn('fast');
}
function OAuthSuccessCallback(authToken) {
$("#pms_token").val(authToken);
$("#pms-token-status").html('<i class="fa fa-check"></i>&nbsp; Authentication successful.').fadeIn('fast');
authenticated = true;
getServerOptions(authToken);
}
function OAuthErrorCallback() {
$("#pms-token-status").html('<i class="fa fa-exclamation-circle"></i>&nbsp; Error communicating with Plex.tv.').fadeIn('fast');
}
clearTimeout(polling);
getPlexOAuthPin().then(function (data) {
const pin = data.pin;
const code = data.code;
var keep_polling = true;
var plex_oauth_window = PopupCenter(
'https://app.plex.tv/auth/#!?clientID=' + x_plex_headers['X-Plex-Client-Identifier'] + '&code=' + code,
'Plex-OAuth', 600, 700);
(function poll() {
polling = setTimeout(function () {
$.ajax({
url: 'https://plex.tv/api/v2/pins/' + pin,
type: 'GET',
headers: x_plex_headers,
success: function (data) {
if (data.authToken){
var authToken = data.authToken;
keep_polling = false;
if (plex_oauth_window) {
plex_oauth_window.close();
}
$("#pms_token").val(authToken);
$("#pms-token-status").html('<i class="fa fa-check"></i>&nbsp; Authentication successful.').fadeIn('fast');
authenticated = true;
getServerOptions(authToken)
}
},
error: function () {
keep_polling = false;
$("#pms-token-status").html('<i class="fa fa-exclamation-circle"></i>&nbsp; Error communicating with Plex.tv.').fadeIn('fast');
},
complete: function () {
if (keep_polling){
poll();
} else {
clearTimeout(polling);
}
},
timeout: 1000
});
}, 1000);
})();
}, function () {
$("#pms-token-status").html('<i class="fa fa-exclamation-circle"></i>&nbsp; Error communicating with Plex.tv.').fadeIn('fast');
});
$('#sign-in-plex').click(function() {
x_plex_headers['X-Plex-Product'] = '${plexpy.common.PRODUCT}';
x_plex_headers['X-Plex-Version'] = '${plexpy.common.RELEASE}';
PlexOAuth(OAuthSuccessCallback, OAuthErrorCallback, OAuthPreFunction);
});
});
</script>