Linted and some consistency refactor on middleware
This commit is contained in:
@@ -1,30 +1,31 @@
|
|||||||
const establishedDatabase = require("../../database/database");
|
const establishedDatabase = require("../../database/database");
|
||||||
|
|
||||||
|
// eslint-disable-next-line consistent-return
|
||||||
const mustBeAdmin = (req, res, next) => {
|
const mustBeAdmin = (req, res, next) => {
|
||||||
const database = establishedDatabase;
|
const database = establishedDatabase;
|
||||||
|
|
||||||
if (req.loggedInUser === undefined) {
|
if (req.loggedInUser === undefined) {
|
||||||
return res.status(401).send({
|
res.status(401).send({
|
||||||
success: false,
|
success: false,
|
||||||
message: "You must be logged in."
|
message: "You must be logged in."
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
database
|
database
|
||||||
.get(
|
.get(
|
||||||
`SELECT admin FROM user WHERE user_name IS ?`,
|
`SELECT admin FROM user WHERE user_name IS ?`,
|
||||||
req.loggedInUser.username
|
req.loggedInUser.username
|
||||||
)
|
)
|
||||||
.then(isAdmin => {
|
.then(isAdmin => {
|
||||||
console.log(isAdmin, req.loggedInUser);
|
if (isAdmin.admin === 0) {
|
||||||
if (isAdmin.admin == 0) {
|
|
||||||
return res.status(401).send({
|
return res.status(401).send({
|
||||||
success: false,
|
success: false,
|
||||||
message: "You must be logged in as a admin."
|
message: "You must be logged in as a admin."
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
return next();
|
return next();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = mustBeAdmin;
|
module.exports = mustBeAdmin;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// eslint-disable-next-line consistent-return
|
||||||
const mustBeAuthenticated = (req, res, next) => {
|
const mustBeAuthenticated = (req, res, next) => {
|
||||||
if (req.loggedInUser === undefined) {
|
if (req.loggedInUser === undefined) {
|
||||||
return res.status(401).send({
|
return res.status(401).send({
|
||||||
@@ -5,7 +6,8 @@ const mustBeAuthenticated = (req, res, next) => {
|
|||||||
message: "You must be logged in."
|
message: "You must be logged in."
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return next();
|
|
||||||
|
next();
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = mustBeAuthenticated;
|
module.exports = mustBeAuthenticated;
|
||||||
|
|||||||
@@ -1,33 +1,36 @@
|
|||||||
const establishedDatabase = require("../../database/database");
|
const establishedDatabase = require("../../database/database");
|
||||||
|
|
||||||
|
/* eslint-disable consistent-return */
|
||||||
const mustHaveAccountLinkedToPlex = (req, res, next) => {
|
const mustHaveAccountLinkedToPlex = (req, res, next) => {
|
||||||
const database = establishedDatabase;
|
const database = establishedDatabase;
|
||||||
const { loggedInUser } = req;
|
const { loggedInUser } = req;
|
||||||
|
|
||||||
if (loggedInUser === undefined) {
|
if (loggedInUser === null) {
|
||||||
return res.status(401).send({
|
return res.status(401).send({
|
||||||
success: false,
|
success: false,
|
||||||
message: "You must have your account linked to a plex account."
|
message: "You must have your account linked to a plex account."
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
database
|
database
|
||||||
.get(
|
.get(
|
||||||
`SELECT plex_userid FROM settings WHERE user_name IS ?`,
|
`SELECT plex_userid FROM settings WHERE user_name IS ?`,
|
||||||
loggedInUser.username
|
loggedInUser.username
|
||||||
)
|
)
|
||||||
.then(row => {
|
.then(row => {
|
||||||
const plexUserId = row?.plex_userid;
|
const plexUserId = row.plex_userid;
|
||||||
|
if (plexUserId === null) {
|
||||||
if (plexUserId === null || plexUserId === undefined) {
|
|
||||||
return res.status(403).send({
|
return res.status(403).send({
|
||||||
success: false,
|
success: false,
|
||||||
message:
|
message:
|
||||||
"No plex account user id found for your user. Please authenticate your plex account at /user/authenticate."
|
"No plex account user id found for your user. Please authenticate your plex account at /user/authenticate."
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
req.loggedInUser.plexUserId = plexUserId;
|
req.loggedInUser.plexUserId = plexUserId;
|
||||||
return next();
|
next();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
/* eslint-enable consistent-return */
|
||||||
|
|
||||||
module.exports = mustHaveAccountLinkedToPlex;
|
module.exports = mustHaveAccountLinkedToPlex;
|
||||||
|
|||||||
@@ -11,22 +11,18 @@ const reqTokenToUser = (req, res, next) => {
|
|||||||
const cookieAuthToken = req.cookies.authorization;
|
const cookieAuthToken = req.cookies.authorization;
|
||||||
const headerAuthToken = req.headers.authorization;
|
const headerAuthToken = req.headers.authorization;
|
||||||
|
|
||||||
if (cookieAuthToken || headerAuthToken) {
|
if (!(cookieAuthToken || headerAuthToken)) {
|
||||||
try {
|
return next();
|
||||||
const token = Token.fromString(
|
|
||||||
cookieAuthToken || headerAuthToken,
|
|
||||||
secret
|
|
||||||
);
|
|
||||||
req.loggedInUser = token.user;
|
|
||||||
} catch (error) {
|
|
||||||
req.loggedInUser = undefined;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// guest session
|
|
||||||
console.debug("No auth token in header or cookie.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
try {
|
||||||
|
const token = Token.fromString(cookieAuthToken || headerAuthToken, secret);
|
||||||
|
req.loggedInUser = token.user;
|
||||||
|
} catch (error) {
|
||||||
|
req.loggedInUser = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return next();
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = reqTokenToUser;
|
module.exports = reqTokenToUser;
|
||||||
|
|||||||
Reference in New Issue
Block a user