updated blogpost controller, auth middleware

This commit is contained in:
ak 2023-09-30 18:39:48 -07:00
parent 0dda4daa7c
commit c657f3cae3
4 changed files with 35 additions and 21 deletions

2
app.js
View file

@ -41,7 +41,7 @@ app.use(
cors({ cors({
origin: true, origin: true,
credentials: true, credentials: true,
methods: ["GET", "PUT", "POST"], methods: ["GET", "PUT", "POST", "DELETE"],
allowedHeaders: [ allowedHeaders: [
"Access-Control-Allow-Headers", "Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept", "Origin, X-Requested-With, Content-Type, Accept",

View file

@ -10,24 +10,26 @@ const sameAuthor = require("../middleware/sameAuthor.js");
exports.index = asyncHandler(async (req, res, next) => { exports.index = asyncHandler(async (req, res, next) => {
// get all posts // get all posts
const dbPosts = await Post.find({ published: true }).lean().exec(); let dbPosts = await Post.find().lean().exec();
// check for authorization // check for authorization
const token = req.cookies.JWT_TOKEN; const token = req.cookies.JWT_TOKEN;
blocc: if (token) { if (token) {
try { const username = jwt.verify(token, process.env.SECRET_KEY).username;
const username = jwt.verify(token, process.env.SECRET_KEY); // filter
} catch { dbPosts.forEach((post, index) => {
break blocc; if (post.published == false) {
} if (post.author != username) {
// run through unpublished posts dbPosts.splice(index, 1);
const unpublished = await Post.find({ published: false }).lean().exec(); }
for (let z = 0; z < unpublished.length; z++) {
const post = unpublished[i];
// if any are by the current user, append to dbPosts
if (username == post.author) {
dbPosts.push(post);
} }
} });
} else {
// filter
dbPosts.forEach((post, index) => {
if (post.published == false) {
dbPosts.splice(index, 1);
}
});
} }
const posts = []; const posts = [];
for (let i = 0; i < dbPosts.length; i++) { for (let i = 0; i < dbPosts.length; i++) {
@ -37,6 +39,7 @@ exports.index = asyncHandler(async (req, res, next) => {
date: dbPosts[i].date, date: dbPosts[i].date,
text: dbPosts[i].text, text: dbPosts[i].text,
author: dbPosts[i].author, author: dbPosts[i].author,
published: dbPosts[i].published,
_id: dbPosts[i]._id, _id: dbPosts[i]._id,
comments: comments, comments: comments,
}; };
@ -108,14 +111,25 @@ exports.get = asyncHandler(async (req, res, next) => {
date: dbPost.date, date: dbPost.date,
text: dbPost.text, text: dbPost.text,
author: dbPost.author, author: dbPost.author,
published: dbPost.published,
_id: dbPost._id, _id: dbPost._id,
comments: comments, comments: comments,
}; };
// if post is not published, not publicly visible // if post is not published, not publicly visible
if (!dbPost.published) { if (!dbPost.published) {
hasToken(); const token = req.cookies.JWT_TOKEN;
sameAuthor(); if (token) {
return res.status(200).json({ post }); const username = jwt.verify(token, process.env.SECRET_KEY).username;
if (username == dbPost.author) {
return res.status(200).json({ post });
}
return res.status(403).json({
message: "Not authorized!",
});
}
return res.status(403).json({
message: "Token not found!",
});
} }
// otherwise return post if published // otherwise return post if published
return res.status(200).json({ post }); return res.status(200).json({ post });

View file

@ -7,7 +7,7 @@ const sameAuthor = asyncHandler(async (req, res, next) => {
// get token // get token
const token = req.cookies.JWT_TOKEN; const token = req.cookies.JWT_TOKEN;
// extract username from token // extract username from token
const username = jwt.verify(token, process.env.SECRET_KEY); const username = jwt.verify(token, process.env.SECRET_KEY).username;
// get post // get post
const post = await Post.findById(req.params.postID).lean().exec(); const post = await Post.findById(req.params.postID).lean().exec();
// if different usernames // if different usernames

View file

@ -4,7 +4,7 @@ const sameUser = (req, res, next) => {
// get token // get token
const token = req.cookies.JWT_TOKEN; const token = req.cookies.JWT_TOKEN;
// extract username from token // extract username from token
const username = jwt.verify(token, process.env.SECRET_KEY); const username = jwt.verify(token, process.env.SECRET_KEY).username;
// if different usernames // if different usernames
if (username != req.params.username) { if (username != req.params.username) {
return res.status(403).json({ return res.status(403).json({