Skip to content

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

Enforce submission is only viewable by author (#1717)

Nick Doanc933f27e22bb
diff --git a/apps/web/src/app/challenge/[slug]/right-wrapper.tsx b/apps/web/src/app/challenge/[slug]/right-wrapper.tsx
index 1040a46d..b8aca4c3 100644
--- a/apps/web/src/app/challenge/[slug]/right-wrapper.tsx
+++ b/apps/web/src/app/challenge/[slug]/right-wrapper.tsx
@@ -28,7 +28,7 @@ export function RightWrapper({ track, challenge, toggleDirection }: RightWrapper
if (!challenge) return null;
if (segments[0] === 'submissions' && typeof segments[1] === 'string') {
- return <SubmissionOverview submissionId={segments[1]} />;
+ return <SubmissionOverview submissionId={segments[1]} userId={session?.user.id ?? ''} />;
}
// Redirect to solution on successful submission and show suggestions
diff --git a/apps/web/src/app/challenge/[slug]/submissions/[[...catchAll]]/_components/overview.tsx b/apps/web/src/app/challenge/[slug]/submissions/[[...catchAll]]/_components/overview.tsx
index e8f760ae..f23c9991 100644
--- a/apps/web/src/app/challenge/[slug]/submissions/[[...catchAll]]/_components/overview.tsx
+++ b/apps/web/src/app/challenge/[slug]/submissions/[[...catchAll]]/_components/overview.tsx
@@ -19,20 +19,21 @@ import { ShareUrl } from '~/components/share-url';
interface SubmissionOverviewProps {
submissionId: string;
+ userId: string;
}
const codifyForMarkdown = (code: string) => {
return `\`\`\`ts
${code}`;
};
-export function SubmissionOverview({ submissionId }: SubmissionOverviewProps) {
+export function SubmissionOverview({ submissionId, userId }: SubmissionOverviewProps) {
const { slug } = useParams();
const searchParams = useSearchParams();
const showSuggestions = searchParams.get('success') === 'true';
const { data: submission } = useQuery({
- queryKey: [`submission`, submissionId],
- queryFn: () => getChallengeSubmissionById(submissionId),
+ queryKey: [`submission`, submissionId, 'userId', userId],
+ queryFn: () => getChallengeSubmissionById(submissionId, userId),
});
const code = codifyForMarkdown(submission?.code.trimStart() ?? '');
@@ -43,7 +44,13 @@ export function SubmissionOverview({ submissionId }: SubmissionOverviewProps) {
`I've completed ${submission?.challenge.name} on TypeHero!`,
)}&url=https://typehero.dev/challenge/${slug}`;
- if (!submission) return null;
+ if (!submission) {
+ return (
+ <div className="flex h-full items-center justify-center text-neutral-500 dark:text-zinc-400">
+ <p>Submission does not exist</p>
+ </div>
+ );
+ }
return (
<>
diff --git a/apps/web/src/app/challenge/[slug]/submissions/[[...catchAll]]/getChallengeSubmissions.ts b/apps/web/src/app/challenge/[slug]/submissions/[[...catchAll]]/getChallengeSubmissions.ts
index a3add10f..1280c6f0 100644
--- a/apps/web/src/app/challenge/[slug]/submissions/[[...catchAll]]/getChallengeSubmissions.ts
+++ b/apps/web/src/app/challenge/[slug]/submissions/[[...catchAll]]/getChallengeSubmissions.ts
@@ -3,10 +3,11 @@
import { cache } from 'react';
import { prisma } from '@repo/db';
-export const getChallengeSubmissionById = cache((id: string) => {
+export const getChallengeSubmissionById = cache((submissionId: string, userId: string) => {
return prisma.submission.findFirst({
where: {
- id: Number(id),
+ id: Number(submissionId),
+ userId,
},
include: {
challenge: {