refactor(api): rename Plex auth endpoint (#949)

This commit is contained in:
TheCatLady
2021-02-23 07:26:21 -05:00
committed by GitHub
parent 970da664b2
commit 09b5019e95
5 changed files with 38 additions and 33 deletions

View File

@@ -26,7 +26,7 @@ authRoutes.get('/me', isAuthenticated(), async (req, res) => {
return res.status(200).json(user);
});
authRoutes.post('/login', async (req, res, next) => {
authRoutes.post('/plex', async (req, res, next) => {
const settings = getSettings();
const userRepository = getRepository(User);
const body = req.body as { authToken?: string };
@@ -35,7 +35,7 @@ authRoutes.post('/login', async (req, res, next) => {
return res.status(500).json({ error: 'You must provide an auth token' });
}
try {
// First we need to use this auth token to get the users email from plex tv
// First we need to use this auth token to get the users email from plex.tv
const plextv = new PlexTvAPI(body.authToken);
const account = await plextv.getUser();
@@ -45,12 +45,12 @@ authRoutes.post('/login', async (req, res, next) => {
});
if (user) {
// Let's check if their plex token is up to date
// Let's check if their Plex token is up-to-date
if (user.plexToken !== body.authToken) {
user.plexToken = body.authToken;
}
// Update the users avatar with their plex thumbnail (incase it changed)
// Update the user's avatar with their Plex thumbnail, in case it changed
user.avatar = account.thumb;
user.email = account.email;
user.plexUsername = account.username;
@@ -80,7 +80,7 @@ authRoutes.post('/login', async (req, res, next) => {
// Double check that we didn't create the first admin user before running this
if (!user) {
// If we get to this point, the user does not already exist so we need to create the
// user _assuming_ they have access to the plex server
// user _assuming_ they have access to the Plex server
const mainUser = await userRepository.findOneOrFail({
select: ['id', 'plexToken'],
order: { id: 'ASC' },
@@ -100,7 +100,7 @@ authRoutes.post('/login', async (req, res, next) => {
await userRepository.save(user);
} else {
logger.info(
'Failed login attempt from user without access to plex server',
'Failed sign-in attempt from user without access to the Plex server.',
{
label: 'Auth',
account: {
@@ -112,7 +112,7 @@ authRoutes.post('/login', async (req, res, next) => {
);
return next({
status: 403,
message: 'You do not have access to this Plex server',
message: 'You do not have access to this Plex server.',
});
}
}
@@ -139,11 +139,11 @@ authRoutes.post('/local', async (req, res, next) => {
const body = req.body as { email?: string; password?: string };
if (!settings.main.localLogin) {
return res.status(500).json({ error: 'Local user login is disabled' });
return res.status(500).json({ error: 'Local user sign-in is disabled.' });
} else if (!body.email || !body.password) {
return res
.status(500)
.json({ error: 'You must provide an email and a password' });
return res.status(500).json({
error: 'You must provide both an email address and a password.',
});
}
try {
const user = await userRepository.findOne({
@@ -155,17 +155,20 @@ authRoutes.post('/local', async (req, res, next) => {
// User doesn't exist or credentials are incorrect
if (!isCorrectCredentials) {
logger.info('Failed login attempt from user with incorrect credentials', {
label: 'Auth',
account: {
ip: req.ip,
email: body.email,
password: '__REDACTED__',
},
});
logger.info(
'Failed sign-in attempt from user with incorrect credentials.',
{
label: 'Auth',
account: {
ip: req.ip,
email: body.email,
password: '__REDACTED__',
},
}
);
return next({
status: 403,
message: 'You do not have access to this Plex server',
message: 'Your sign-in credentials are incorrect.',
});
}
@@ -176,7 +179,7 @@ authRoutes.post('/local', async (req, res, next) => {
return res.status(200).json(user?.filter() ?? {});
} catch (e) {
logger.error('Something went wrong when trying to authenticate', {
logger.error('Something went wrong while attempting to authenticate.', {
label: 'Auth',
error: e.message,
});
@@ -205,7 +208,9 @@ authRoutes.post('/reset-password', async (req, res) => {
const body = req.body as { email?: string };
if (!body.email) {
return res.status(500).json({ error: 'You must provide an email' });
return res
.status(500)
.json({ error: 'You must provide an email address.' });
}
const user = await userRepository.findOne({
@@ -215,12 +220,12 @@ authRoutes.post('/reset-password', async (req, res) => {
if (user) {
await user.resetPassword();
userRepository.save(user);
logger.info('Successful request made for recovery link', {
logger.info('Successful request made for recovery link.', {
label: 'User Management',
context: { ip: req.ip, email: body.email },
});
} else {
logger.info('Failed request made to reset a password', {
logger.info('Failed request made to reset a password.', {
label: 'User Management',
context: { ip: req.ip, email: body.email },
});
@@ -235,7 +240,7 @@ authRoutes.post('/reset-password/:guid', async (req, res, next) => {
try {
if (!req.body.password || req.body.password?.length < 8) {
const message =
'Failed to reset password. Password must be atleast 8 characters long.';
'Failed to reset password. Password must be at least 8 characters long.';
logger.info(message, {
label: 'User Management',
context: { ip: req.ip, guid: req.params.guid },