Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dzencot committed Sep 18, 2024
1 parent 6d78a98 commit 150f384
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions server/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const app = async (host, port) => {
return res.status(404).send({ code: 404, message: 'Not found' });
}
const currentUser = getUserByToken(c, state);
if (currentUser.id !== id) {
if (currentUser?.id !== id) {
return res
.status(403)
.send({ code: 403, message: 'Forbidden action' })
Expand All @@ -104,7 +104,7 @@ const app = async (host, port) => {
UserService_delete: (c, req, res) => {
const { id } = c.request.params;
const currentUser = getUserByToken(c, state);
if (currentUser.id !== id) {
if (currentUser?.id !== id) {
return res
.status(403)
.send({ code: 403, message: 'Forbidden action' })
Expand All @@ -121,6 +121,11 @@ const app = async (host, port) => {
body,
} = c.request.body;
const currentUser = getUserByToken(c, state);
if (currentUser) {
return res
.status(403)
.send({ code: 403, message: 'Forbidden action' })
}
const post = {
id: getId(),
authorId: currentUser.id,
Expand Down Expand Up @@ -153,7 +158,7 @@ const app = async (host, port) => {
return res.status(404).send({ code: 404, message: 'Not found' });
}
const currentUser = getUserByToken(c, state);
if (currentUser.id !== state.posts[index].authorId) {
if (currentUser?.id !== state.posts[index].authorId) {
return res
.status(403)
.send({ code: 403, message: 'Forbidden action' })
Expand All @@ -168,7 +173,7 @@ const app = async (host, port) => {
const { id } = c.request.params;
const index = state.posts.findIndex((item) => item.id === id);
const currentUser = getUserByToken(c, state);
if (currentUser.id !== state.posts[index].authorId) {
if (currentUser?.id !== state.posts[index].authorId) {
return res
.status(403)
.send({ code: 403, message: 'Forbidden action' })
Expand All @@ -185,6 +190,11 @@ const app = async (host, port) => {
body,
} = c.request.body;
const currentUser = getUserByToken(c, state);
if (!currentUser) {
return res
.status(403)
.send({ code: 403, message: 'Forbidden action' })
}
const comment = {
id: getId(),
postId,
Expand Down Expand Up @@ -212,7 +222,7 @@ const app = async (host, port) => {
return res.status(404).send({ code: 404, message: 'Not found' });
}
const currentUser = getUserByToken(c, state);
if (currentUser.id !== state.comments[index].authorId) {
if (currentUser?.id !== state.comments[index].authorId) {
return res
.status(403)
.send({ code: 403, message: 'Forbidden action' })
Expand All @@ -227,7 +237,7 @@ const app = async (host, port) => {
const { id } = c.request.params;
const index = state.comments.findIndex((item) => item.id === id);
const currentUser = getUserByToken(c, state);
if (currentUser.id !== state.comments[index].authorId) {
if (currentUser?.id !== state.comments[index].authorId) {
return res
.status(403)
.send({ code: 403, message: 'Forbidden action' })
Expand Down

0 comments on commit 150f384

Please sign in to comment.