mirror of
https://github.com/mjrode/WhatToWatch.git
synced 2025-12-30 18:19:46 -06:00
Add auth module.
This commit is contained in:
27
server/services/plexApi/auth.js
Normal file
27
server/services/plexApi/auth.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import uuid from 'uuid/v1';
|
||||
import btoa from 'btoa';
|
||||
import request from 'request-promise';
|
||||
|
||||
const rxAuthToken = /authenticationToken="([^"]+)"/;
|
||||
|
||||
const options = (username, password) => ({
|
||||
url: 'https://plex.tv/users/sign_in.xml',
|
||||
headers: {
|
||||
'X-Plex-Client-Identifier': uuid(),
|
||||
Authorization: `Basic ${encryptUserCreds(username, password)}`,
|
||||
},
|
||||
});
|
||||
|
||||
const encryptUserCreds = (username, password) => {
|
||||
const creds = `${username}:${password}`;
|
||||
return btoa(creds);
|
||||
};
|
||||
|
||||
const fetchToken = (username, password) => {
|
||||
request.post(options(username, password)).then(res => {
|
||||
const token = res.match(rxAuthToken)[1];
|
||||
return token;
|
||||
});
|
||||
};
|
||||
|
||||
export default fetchToken;
|
||||
Reference in New Issue
Block a user