Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

refactor: user validation on get specific document

Marc Seitzcb0e524d900b
diff --git a/pages/api/documents/[id]/index.ts b/pages/api/documents/[id]/index.ts
index 8633e28b..2c89775a 100644
--- a/pages/api/documents/[id]/index.ts
+++ b/pages/api/documents/[id]/index.ts
@@ -2,6 +2,7 @@ import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import prisma from "@/lib/prisma";
import { authOptions } from "../../auth/[...nextauth]";
+import { CustomUser } from "@/lib/types";
export default async function handle(
req: NextApiRequest,
@@ -23,9 +24,16 @@ export default async function handle(
},
});
- // TODO: Check that the user is owner of the document, otherwise return 401
+ if (!document) {
+ return res.status(404).end("Document not found");
+ }
- res.status(200).json(document);
+ // Check that the user is owner of the document, otherwise return 401
+ if (document.ownerId !== (session.user as CustomUser).id) {
+ return res.status(401).end("Unauthorized to access this document");
+ }
+
+ return res.status(200).json(document);
} catch (error) {
return res.status(500).json({
message: "Internal Server Error",