Adding starter functions

This commit is contained in:
luke-hagar-sp
2022-07-22 16:35:43 -05:00
parent 9230ab4b7d
commit 5367764043
3 changed files with 165 additions and 125 deletions

164
index.js Normal file
View File

@@ -0,0 +1,164 @@
import { PlexOauth, IPlexClientDetails } from "plex-oauth";
import v4 from "uuid/dist/v4";
import axios from "axios";
import qs from "qs";
var PlexAPIOauth = /** @class */ function (
clientId,
product = "Plex-API-OAuth",
device = "Web Client",
version = "1",
forwardUrl = "",
platform = "Web"
) {
if (clientId === null) {
var clientId = localStorage.getItem("plex-client-id"); //Defaults to last used ClientId from any previous runs
}
if (clientId === null) {
const uuid = v4(); //If no ClientId is saved, generate a new one and save it
clientId = uuid;
}
localStorage.setItem("plex-client-id", clientId);
console.log("Plex ClientID:");
console.log(clientId);
var plexData = localStorage.getItem("plex-database"); //Retrieve plexData from localStorage if saved from previous actions
if (plexData === null) {
plexData = {
plexAuthToken: "",
plexUserData: {},
plexServers: {},
plexLibraries: {},
plexMovies: {},
plexMusic: {},
plexTVShows: {},
};
}
console.log("Plex DataBase:");
console.log(plexData);
function openInNewTab(url) {
var separateWindow = window.open(url, "_blank");
separateWindow?.focus;
}
var clientInformation = {
clientIdentifier: clientId, // This is a unique identifier used to identify your app with Plex. - If none is provided a new one is generated and saved locally
product: product, // Name of your application - Defaults to Plex-API-OAuth
device: device, // The type of device your application is running on - Defaults to "Web Client"
version: version, // Version of your application - Defaults to 1
forwardUrl: forwardUrl, // Url to forward back to after signing in - Defaults to an empty string
platform: platform, // Platform your application runs on - Defaults to 'Web'
};
var plexOauth = new PlexOauth(clientInformation);
// Get hosted UI URL and Pin Id
async function plexLogin() {
await plexOauth
.requestHostedLoginURL()
.then((data) => {
let [hostedUILink, pinId] = data;
console.log("Plex Auth URL:");
console.log(hostedUILink); // UI URL used to log into Plex
console.log("Plex Pin ID:");
console.log(pinId);
openInNewTab(hostedUILink);
/*
* You can now navigate the user's browser to the 'hostedUILink'. This will include the forward URL
* for your application, so when they have finished signing into Plex, they will be redirected back
* to the specified URL. From there, you just need to perform a query to check for the auth token.
* (See Below)
*/
// Check for the auth token, once returning to the application
plexOauth
.checkForAuthToken(pinId, 1000, 10)
.then((authToken) => {
console.log("Plex Auth Token:");
console.log(authToken); // Returns the auth token if set, otherwise returns null
if (authToken !== null) {
plexData.plexAuthToken = authToken;
} else {
console.log("No Authentication Token returned from Plex");
}
// An auth token will only be null if the user never signs into the hosted UI, or you stop checking for a new one before they can log in
})
.catch((err) => {
throw err;
});
})
.catch((err) => {
throw err;
});
}
async function validatePlexAuthToken() {
return await axios({
method: "GET",
url:
"https://plex.tv/api/v2/user?" +
require("qs").stringify({
"X-Plex-Product": clientInformation.product,
"X-Plex-Client-Identifier": clientInformation.clientId,
"X-Plex-Token": plexData.plexAuthToken,
}),
headers: { accept: "application/json" },
})
.then((response) => {
console.log(response);
console.log(response.status);
if (response.status === 200) {
plexData.plexUserData = response.data;
return true;
}
if (response.status === 401) {
console.log("Authentican Token Failed Validation");
return false;
}
})
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log("Error", error.message);
}
console.log(error.config);
});
}
function authToken() {
return plexData.plexAuthToken;
}
function plexData() {
return plexData;
}
function plexUserData() {
return plexData.plexUserData;
}
function plexServers() {
return plexData.plexServers;
}
function plexLibraries() {
return plexData.plexLibraries;
}
};
module.exports = {
PlexAPIOauth: PlexAPIOauth,
};

124
index.ts
View File

@@ -1,124 +0,0 @@
import { PlexOauth, IPlexClientDetails } from "plex-oauth"
import v4 from "uuid/dist/v4";
import axios from 'axios';
import qs from 'qs'
var PlexOauth = /** @class */ (function () {
var clientId = localStorage.getItem('plex-client-id')
if (clientId === null) {
const uuid = v4();
localStorage.setItem('plex-client-id', uuid);
clientId = uuid;
};
const plexData = (localStorage.getItem('plex-database'))
console.log(plexData)
function openInNewTab(url: string | URL | undefined) {
let separateWindow = window.open(url, '_blank');
separateWindow?.focus;
}
let clientInformation: IPlexClientDetails = {
clientIdentifier: "<PROVIDE_UNIQUE_VALUE>", // This is a unique identifier used to identify your app with Plex.
product: "<NAME_OF_YOUR_APP>", // Name of your application
device: "<NAME_OF_YOUR_DEVICE>", // The type of device your application is running on
version: "1", // Version of your application
forwardUrl: "https://localhost:3000", // Url to forward back to after signing in.
platform: "Web", // Optional - Platform your application runs on - Defaults to 'Web'
}
let plexOauth = new PlexOauth(clientInformation);
// Get hosted UI URL and Pin Id
function plexLogin() {
plexOauth
.requestHostedLoginURL()
.then((data) => {
let [hostedUILink, pinId] = data;
console.log('Plex Auth URL:');
console.log(hostedUILink); // UI URL used to log into Plex
console.log('Plex Pin ID:');
console.log(pinId);
openInNewTab(hostedUILink);
/*
* You can now navigate the user's browser to the 'hostedUILink'. This will include the forward URL
* for your application, so when they have finished signing into Plex, they will be redirected back
* to the specified URL. From there, you just need to perform a query to check for the auth token.
* (See Below)
*/
// Check for the auth token, once returning to the application
plexOauth
.checkForAuthToken(pinId, 1000, 10)
.then((authToken) => {
console.log('Plex Auth Token:');
console.log(authToken); // Returns the auth token if set, otherwise returns null
if (authToken !== null) {
validatePlexAuthToken(authToken);
}
else{ console.log("No Authentication Token returned from Plex")}
// An auth token will only be null if the user never signs into the hosted UI, or you stop checking for a new one before they can log in
})
.catch((err) => {
throw err;
});
})
.catch((err) => {
throw err;
});
}
function validatePlexAuthToken(authToken) {
axios({
method: 'GET',
url:
'https://plex.tv/api/v2/user?' +
require('qs').stringify({
'X-Plex-Product': clientInformation.product,
'X-Plex-Client-Identifier': clientId,
'X-Plex-Token': authToken,
}),
headers: { accept: 'application/json' },
})
.then((response) => {
console.log(response);
console.log(response.status);
if (response.status === 200) {
let tempData = plexData;
tempData.plexUserData = response.data;
tempData.plexAuthToken = authToken;
setPlexData(tempData);
getPlexServers();
getPlexLibraries();
getPlexMusicLibraries();
savePlexState();
}
if (response.status === 401) {
}
})
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});
}}

View File

@@ -1,6 +1,6 @@
{
"name": "plex-api-oauth",
"version": "1.0.0",
"version": "1.0.1",
"description": "An NPM Module designed to make Plex Media Server and plex.tv API calls easier to implement in JavaScript and React projects",
"main": "index.js",
"scripts": {