Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

Ensure User is the author of a comment before deleting (#1044)

Zach Lanktonb4cf17056473
diff --git a/apps/web/src/app/[locale]/challenge/_components/comments/comment.action.ts b/apps/web/src/app/[locale]/challenge/_components/comments/comment.action.ts
index ddb56363..f552197a 100644
--- a/apps/web/src/app/[locale]/challenge/_components/comments/comment.action.ts
+++ b/apps/web/src/app/[locale]/challenge/_components/comments/comment.action.ts
@@ -65,17 +65,27 @@ export async function replyComment(comment: CommentToCreate, parentId: number) {
export async function updateComment(text: string, id: number) {
const session = await getServerAuthSession();
- if (!session?.user.id) return 'unauthorized';
+ if (!session) return 'unauthorized';
if (text.length === 0) return 'text_is_empty';
- if (!session.user.id) return 'unauthorized';
+
+ const comment = await prisma.comment.findFirstOrThrow({
+ where: {
+ id,
+ },
+ });
+
+ const isAuthorized = isAdminOrModerator(session) || isAuthor(session, comment.userId);
+ if (!isAuthorized) {
+ return 'unauthorized';
+ }
return await prisma.comment.update({
where: {
id,
+ userId: session.user.id,
},
data: {
text,
- userId: session.user.id,
},
});
}
@@ -85,20 +95,23 @@ export async function updateComment(text: string, id: number) {
* @param author The ID of the user who authored the comment.
* @returns 'unauthorized' if the user is not authorized, 'invalid_comment' if the comment ID is not provided, or undefined if the comment is successfully deleted.
*/
-export async function deleteComment(comment_id: number, author: string) {
+export async function deleteComment(comment_id: number) {
const session = await getServerAuthSession();
- if (!session?.user.id) return 'unauthorized';
+
+ if (!session) return 'unauthorized';
if (!comment_id) return 'invalid_comment';
- const isAuthorized = isAdminOrModerator(session) || isAuthor(session, author);
- if (!isAuthorized) {
- return 'unauthorized';
- }
+
const rootComment = await prisma.comment.findFirstOrThrow({
where: {
id: comment_id,
},
});
+ const isAuthorized = isAdminOrModerator(session) || isAuthor(session, rootComment.userId);
+ if (!isAuthorized) {
+ return 'unauthorized';
+ }
+
await deleteCommentWithChildren(prisma, rootComment);
}
diff --git a/apps/web/src/app/[locale]/challenge/_components/comments/delete/index.tsx b/apps/web/src/app/[locale]/challenge/_components/comments/delete/index.tsx
index 3518eb99..cb7c37e7 100644
--- a/apps/web/src/app/[locale]/challenge/_components/comments/delete/index.tsx
+++ b/apps/web/src/app/[locale]/challenge/_components/comments/delete/index.tsx
@@ -28,12 +28,17 @@ export function CommentDeleteDialog({
async function handleDeleteComment() {
try {
- const res = await deleteComment(comment.id, comment.userId);
+ const res = await deleteComment(comment.id);
if (res === 'unauthorized') {
toast({
title: 'Unauthorized',
description: <p>You need to be signed in to post a comment.</p>,
});
+ } else if (res === 'invalid_comment') {
+ toast({
+ title: 'Invalid Comment',
+ description: 'The comment id is invalid.',
+ });
} else {
toast({
title: 'Comment Deleted',