diff --git a/backend/package-lock.json b/backend/package-lock.json index dcf46bd..16845a7 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -8,6 +8,7 @@ "dependencies": { "@prisma/client": "^5.19.1", "@prisma/extension-accelerate": "^1.1.0", + "@pushkar1713/week13-common": "^1.0.1", "hono": "^4.5.11", "prisma": "^5.19.1" }, @@ -611,6 +612,14 @@ "@prisma/debug": "5.19.1" } }, + "node_modules/@pushkar1713/week13-common": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@pushkar1713/week13-common/-/week13-common-1.0.1.tgz", + "integrity": "sha512-2Ws0BG52cgQtC7trmFdnbOxXwKpw2AQwVBmoa0uwRWfzTFBAAWskpFq71jkTpUEDewfYWjRjCrY7s6ShJm7Uuw==", + "dependencies": { + "zod": "^3.23.8" + } + }, "node_modules/@types/node": { "version": "22.5.4", "resolved": "https://registry.npmjs.org/@types/node/-/node-22.5.4.tgz", @@ -1441,7 +1450,6 @@ "version": "3.23.8", "resolved": "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz", "integrity": "sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==", - "dev": true, "funding": { "url": "https://github.com/sponsors/colinhacks" } diff --git a/backend/package.json b/backend/package.json index 6692a8b..740cde8 100644 --- a/backend/package.json +++ b/backend/package.json @@ -8,6 +8,7 @@ "dependencies": { "@prisma/client": "^5.19.1", "@prisma/extension-accelerate": "^1.1.0", + "@pushkar1713/week13-common": "^1.0.1", "hono": "^4.5.11", "prisma": "^5.19.1" }, diff --git a/backend/src/routes/posts.ts b/backend/src/routes/posts.ts index ec0690c..a7a36ea 100644 --- a/backend/src/routes/posts.ts +++ b/backend/src/routes/posts.ts @@ -1,7 +1,7 @@ import { Hono } from "hono"; import { getPrisma } from "../prismaFunction"; import { decode, sign, verify } from "hono/jwt"; - +import { createPost, updatePost } from "@pushkar1713/week13-common";} export const postRouter = new Hono<{ Bindings: { DATABASE_URL: string; @@ -40,6 +40,13 @@ postRouter.use("/*", async (c, next) => { postRouter.post("/", async (c) => { const prisma = getPrisma(c.env.DATABASE_URL); const body = await c.req.json(); + const { success } = createPost.safeParse(body); + if (!success) { + c.status(403); + return c.json({ + msg: "invalid types", + }); + } const userId = c.get("userId"); const blog = await prisma.post.create({ @@ -84,6 +91,13 @@ postRouter.get("/:id", async (c) => { postRouter.put("/", async (c) => { const prisma = getPrisma(c.env.DATABASE_URL); const body = await c.req.json(); + const { success } = updatePost.safeParse(body); + if (!success) { + c.status(403); + return c.json({ + msg: "invalid types", + }); + } const updatedBlog = await prisma.post.update({ where: { diff --git a/backend/src/routes/user.ts b/backend/src/routes/user.ts index 58421d8..1c00fab 100644 --- a/backend/src/routes/user.ts +++ b/backend/src/routes/user.ts @@ -1,6 +1,7 @@ import { Hono } from "hono"; import { getPrisma } from "../prismaFunction"; import { decode, sign, verify } from "hono/jwt"; +import { signinInput, signupInput } from "@pushkar1713/week13-common"; export const userRouter = new Hono<{ Bindings: { @@ -12,6 +13,13 @@ export const userRouter = new Hono<{ userRouter.post("/signup", async (c) => { const prisma = getPrisma(c.env.DATABASE_URL); const body = await c.req.json(); + const { success } = signupInput.safeParse(body); + if (!success) { + c.status(403); + return c.json({ + msg: "invalid types", + }); + } try { const user = await prisma.user.create({ @@ -38,6 +46,13 @@ userRouter.post("/signup", async (c) => { userRouter.post("/signin", async (c) => { const prisma = getPrisma(c.env.DATABASE_URL); const body = await c.req.json(); + const { success } = signinInput.safeParse(body); + if (!success) { + c.status(403); + return c.json({ + msg: "invalid types", + }); + } try { const user = prisma.user.findUnique({