Connect, collaborate, and grow with a community of TypeScript developers
Feat :heavy_plus_sign: : Implement comment deletion authorization (#841)
Vivek Kumar42338d70ff9a
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.tsindex 3e7c7d52..ddb56363 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@@ -3,6 +3,7 @@import { getServerAuthSession } from '@repo/auth/server';import { prisma } from '@repo/db';import type { Comment, CommentRoot, PrismaClient } from '@repo/db/types';+import { isAdminOrModerator, isAuthor } from '~/utils/auth-guards';/***@@ -79,16 +80,19 @@ export async function updateComment(text: string, id: number) {});}/**- * Delete's a comment given a comment id. It must- * be your own comment.- * @props comment_id The id of the comment.+ * Deletes a comment given a comment ID. The user must be the author of the comment or have the role of 'ADMIN' or 'MODERATOR'.+ * @param comment_id The ID of the comment to be deleted.+ * @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) {+export async function deleteComment(comment_id: number, author: string) {const session = await getServerAuthSession();-if (!session?.user.id) 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,diff --git a/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx b/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsxindex 360aa43a..6dbe6956 100644--- a/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx+++ b/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx@@ -37,6 +37,7 @@ import {import { Avatar, AvatarFallback, AvatarImage } from '@repo/ui/components/avatar';import { Button } from '@repo/ui/components/button';import { CommentSkeleton } from './comment-skeleton';+import { isAdminOrModerator } from '~/utils/auth-guards';interface SingleCommentProps {comment: PaginatedComments['comments'][number];@@ -340,6 +341,7 @@ function SingleComment({const loggedinUser = useSession();const isAuthor = loggedinUser.data?.user.id === comment.user.id;+ const isAdminAndModerator = isAdminOrModerator(loggedinUser.data);useEffect(() => {if (!isHighlighted) return;@@ -472,7 +474,7 @@ function SingleComment({</TooltipContent></Tooltip>) : null}- {isAuthor ? (+ {isAuthor || isAdminAndModerator ? (<Tooltip><CommentDeleteDialog asChild comment={comment}><TooltipTrigger asChild>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.tsxindex ff18024d..3518eb99 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,19 @@ export function CommentDeleteDialog({async function handleDeleteComment() {try {- await deleteComment(comment.id);- toast({- title: 'Comment Deleted',- variant: 'success',- description: 'The comment was successfully deleted.',- });+ const res = await deleteComment(comment.id, comment.userId);+ if (res === 'unauthorized') {+ toast({+ title: 'Unauthorized',+ description: <p>You need to be signed in to post a comment.</p>,+ });+ } else {+ toast({+ title: 'Comment Deleted',+ variant: 'success',+ description: 'The comment was successfully deleted.',+ });+ }} catch (e) {// todo: log on a dump service.console.log(e);