Correctly use username from req.loggedInUser
This commit is contained in:
@@ -1,24 +1,25 @@
|
||||
const UserRepository = require('src/user/userRepository');
|
||||
const UserRepository = require("src/user/userRepository");
|
||||
const userRepository = new UserRepository();
|
||||
const fetch = require('node-fetch');
|
||||
const FormData = require('form-data');
|
||||
const fetch = require("node-fetch");
|
||||
const FormData = require("form-data");
|
||||
|
||||
function handleError(error, res) {
|
||||
let { status, message, source } = error;
|
||||
|
||||
if (status && message) {
|
||||
if (status === 401) {
|
||||
message = 'Unauthorized. Please check plex credentials.',
|
||||
source = 'plex'
|
||||
(message = "Unauthorized. Please check plex credentials."),
|
||||
(source = "plex");
|
||||
}
|
||||
|
||||
res.status(status).send({ success: false, message, source })
|
||||
res.status(status).send({ success: false, message, source });
|
||||
} else {
|
||||
console.log('caught authenticate plex account controller error', error)
|
||||
console.log("caught authenticate plex account controller error", error);
|
||||
res.status(500).send({
|
||||
message: 'An unexpected error occured while authenticating your account with plex',
|
||||
message:
|
||||
"An unexpected error occured while authenticating your account with plex",
|
||||
source
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,33 +29,32 @@ function handleResponse(response) {
|
||||
success: false,
|
||||
status: response.status,
|
||||
message: response.statusText
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return response.json()
|
||||
return response.json();
|
||||
}
|
||||
|
||||
function plexAuthenticate(username, password) {
|
||||
const url = 'https://plex.tv/api/v2/users/signin'
|
||||
const url = "https://plex.tv/api/v2/users/signin";
|
||||
|
||||
const form = new FormData()
|
||||
form.append('login', username)
|
||||
form.append('password', password)
|
||||
form.append('rememberMe', 'false')
|
||||
const form = new FormData();
|
||||
form.append("login", username);
|
||||
form.append("password", password);
|
||||
form.append("rememberMe", "false");
|
||||
|
||||
const headers = {
|
||||
'Accept': 'application/json, text/plain, */*',
|
||||
'Content-Type': form.getHeaders()['content-type'],
|
||||
'X-Plex-Client-Identifier': 'seasonedRequest'
|
||||
}
|
||||
Accept: "application/json, text/plain, */*",
|
||||
"Content-Type": form.getHeaders()["content-type"],
|
||||
"X-Plex-Client-Identifier": "seasonedRequest"
|
||||
};
|
||||
const options = {
|
||||
method: 'POST',
|
||||
method: "POST",
|
||||
headers,
|
||||
body: form
|
||||
}
|
||||
};
|
||||
|
||||
return fetch(url, options)
|
||||
.then(resp => handleResponse(resp))
|
||||
return fetch(url, options).then(resp => handleResponse(resp));
|
||||
}
|
||||
|
||||
function link(req, res) {
|
||||
@@ -63,22 +63,28 @@ function link(req, res) {
|
||||
|
||||
return plexAuthenticate(username, password)
|
||||
.then(plexUser => userRepository.linkPlexUserId(user.username, plexUser.id))
|
||||
.then(response => res.send({
|
||||
success: true,
|
||||
message: "Successfully authenticated and linked plex account with seasoned request."
|
||||
}))
|
||||
.catch(error => handleError(error, res))
|
||||
.then(response =>
|
||||
res.send({
|
||||
success: true,
|
||||
message:
|
||||
"Successfully authenticated and linked plex account with seasoned request."
|
||||
})
|
||||
)
|
||||
.catch(error => handleError(error, res));
|
||||
}
|
||||
|
||||
function unlink(req, res) {
|
||||
const user = req.loggedInUser;
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
return userRepository.unlinkPlexUserId(user.username)
|
||||
.then(response => res.send({
|
||||
success: true,
|
||||
message: "Successfully unlinked plex account from seasoned request."
|
||||
}))
|
||||
.catch(error => handleError(error, res))
|
||||
return userRepository
|
||||
.unlinkPlexUserId(username)
|
||||
.then(response =>
|
||||
res.send({
|
||||
success: true,
|
||||
message: "Successfully unlinked plex account from seasoned request."
|
||||
})
|
||||
)
|
||||
.catch(error => handleError(error, res));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const RequestRepository = require('src/plex/requestRepository.js');
|
||||
const RequestRepository = require("src/plex/requestRepository.js");
|
||||
|
||||
const requestRepository = new RequestRepository();
|
||||
|
||||
@@ -9,15 +9,20 @@ const requestRepository = new RequestRepository();
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function requestsController(req, res) {
|
||||
const user = req.loggedInUser;
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
requestRepository.userRequests(user)
|
||||
.then(requests => {
|
||||
res.send({ success: true, results: requests, total_results: requests.length });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).send({ success: false, message: error.message });
|
||||
requestRepository
|
||||
.userRequests(username)
|
||||
.then(requests => {
|
||||
res.send({
|
||||
success: true,
|
||||
results: requests,
|
||||
total_results: requests.length
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = requestsController;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const SearchHistory = require('src/searchHistory/searchHistory');
|
||||
const SearchHistory = require("src/searchHistory/searchHistory");
|
||||
|
||||
const searchHistory = new SearchHistory();
|
||||
|
||||
@@ -9,16 +9,16 @@ const searchHistory = new SearchHistory();
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function historyController(req, res) {
|
||||
const user = req.loggedInUser;
|
||||
const username = user === undefined ? undefined : user.username;
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
searchHistory.read(username)
|
||||
.then(searchQueries => {
|
||||
res.send({ success: true, searchQueries });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(404).send({ success: false, message: error.message });
|
||||
});
|
||||
searchHistory
|
||||
.read(username)
|
||||
.then(searchQueries => {
|
||||
res.send({ success: true, searchQueries });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(404).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = historyController;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const UserRepository = require('src/user/userRepository');
|
||||
const UserRepository = require("src/user/userRepository");
|
||||
const userRepository = new UserRepository();
|
||||
/**
|
||||
* Controller: Retrieves settings of a logged in user
|
||||
@@ -7,36 +7,35 @@ const userRepository = new UserRepository();
|
||||
* @returns {Callback}
|
||||
*/
|
||||
const getSettingsController = (req, res) => {
|
||||
const user = req.loggedInUser;
|
||||
const username = user === undefined ? undefined : user.username;
|
||||
|
||||
userRepository.getSettings(username)
|
||||
.then(settings => {
|
||||
res.send({ success: true, settings });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(404).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
userRepository
|
||||
.getSettings(username)
|
||||
.then(settings => {
|
||||
res.send({ success: true, settings });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(404).send({ success: false, message: error.message });
|
||||
});
|
||||
};
|
||||
|
||||
const updateSettingsController = (req, res) => {
|
||||
const user = req.loggedInUser;
|
||||
const username = user === undefined ? undefined : user.username;
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
|
||||
const idempotencyKey = req.headers('Idempotency-Key'); // TODO implement better transactions
|
||||
const { dark_mode, emoji } = req.body;
|
||||
const idempotencyKey = req.headers("Idempotency-Key"); // TODO implement better transactions
|
||||
const { dark_mode, emoji } = req.body;
|
||||
|
||||
userRepository.updateSettings(username, dark_mode, emoji)
|
||||
.then(settings => {
|
||||
res.send({ success: true, settings });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(404).send({ success: false, message: error.message });
|
||||
});
|
||||
}
|
||||
userRepository
|
||||
.updateSettings(username, dark_mode, emoji)
|
||||
.then(settings => {
|
||||
res.send({ success: true, settings });
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(404).send({ success: false, message: error.message });
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getSettingsController,
|
||||
updateSettingsController
|
||||
}
|
||||
getSettingsController,
|
||||
updateSettingsController
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user