Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Security Fix: Deny users from editing others posts. #48

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/app/(main)/editor/[postId]/_components/post-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ import { api } from "@/trpc/react";
import { Pencil2Icon } from "@/components/icons";
import { LoadingButton } from "@/components/loading-button";
import Link from "next/link";
import { toast } from "sonner";

const markdownlink = "https://remarkjs.github.io/react-markdown/" // Can also be changed for something like /markdown

type usertype = {
id: string;
}

interface Props {
post: RouterOutputs["post"]["get"];
user: usertype;
}

const schema = z.object({
Expand All @@ -37,7 +43,7 @@ const schema = z.object({
.max(2048 * 2),
});

export const PostEditor = ({ post }: Props) => {
export const PostEditor = ({ post, user }: Props) => {
if (!post) return null;
const formRef = useRef<HTMLFormElement>(null);
const updatePost = api.post.update.useMutation();
Expand All @@ -50,8 +56,16 @@ export const PostEditor = ({ post }: Props) => {
resolver: zodResolver(schema),
});
const onSubmit = form.handleSubmit(async (values) => {
updatePost.mutate({ id: post.id, ...values });
if (user.id == post.userId) {
updatePost.mutate({ id: post.id, ...values });
toast('Saved the post successfully.');
return;
}

toast('You do not have permission to edit this post.');
return;
});


return (
<>
Expand Down
2 changes: 1 addition & 1 deletion src/app/(main)/editor/[postId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default async function EditPostPage({ params }: Props) {
<ArrowLeftIcon className="h-5 w-5" /> back to dashboard
</Link>

<PostEditor post={post} />
<PostEditor post={post} user={user} />
</main>
);
}
Loading