fix: userId from req.body wtf

This commit is contained in:
Florian Sylvain
2024-10-10 22:27:36 +02:00
parent 4b79e37345
commit 307d7212e0
4 changed files with 26 additions and 14 deletions
+15 -11
View File
@@ -2,7 +2,7 @@ import { Category, Prisma, PrismaClient } from "@prisma/client"
import express, { RequestHandler } from "express"
import { z } from "zod"
import { getPaginationLinks, Pagination, queryPaginationParser } from "../utils/pagination.js"
import { userIdMiddleware } from "./user.js"
import { parseJwtUserId, userIdMiddleware } from "./user.js"
const categoryRouter = express.Router()
const prisma = new PrismaClient()
@@ -37,7 +37,8 @@ const categoryGet: RequestHandler = async (req, res) => {
const pagination = queryPaginationParser.parse(req.query)
try {
categories = await findCategories(req.body.userId, pagination)
const userId = parseJwtUserId(req.cookies.jwt)
categories = await findCategories(userId ?? -1, pagination)
} catch (error: any) {
res.status(400).json({ message: error.issues ?? error })
return
@@ -55,10 +56,11 @@ const categoryGetUnique: RequestHandler = async (req, res) => {
let category: Category | null = null
try {
const userId = parseJwtUserId(req.cookies.jwt)
category = await prisma.category.findFirst({
where: {
id: paramsIdParser.parse(req.params).id,
user_id: req.body.userId,
user_id: userId,
},
})
} catch (error: any) {
@@ -71,19 +73,19 @@ const categoryGetUnique: RequestHandler = async (req, res) => {
const categoryPost: RequestHandler = async (req, res) => {
try {
const newCategory = categoryPostParser.parse(req.body)
await prisma.category.create({
const userId = parseJwtUserId(req.cookies.jwt)
const parsedCategory = categoryPostParser.parse(req.body)
const newCategorey = await prisma.category.create({
data: {
name: newCategory.name,
user_id: req.body.userId,
name: parsedCategory.name,
user_id: userId ?? -1,
},
})
res.json({ message: "Category successfully added.", id: newCategorey.id })
} catch (error: any) {
res.status(400).json({ message: error.issues ?? error })
return
}
res.json({ message: "Category successfully added." })
}
const categoryUpdate: RequestHandler = async (req, res) => {
@@ -91,10 +93,11 @@ const categoryUpdate: RequestHandler = async (req, res) => {
try {
const categoryToUpdate = categoryUpdateParser.parse(req.body)
const userId = parseJwtUserId(req.cookies.jwt)
updated = await prisma.category.updateMany({
where: {
id: paramsIdParser.parse(req.params).id,
user_id: req.body.userId,
user_id: userId,
},
data: {
name: categoryToUpdate.name,
@@ -112,10 +115,11 @@ const categoryDelete: RequestHandler = async (req, res) => {
let deleted: Prisma.BatchPayload
try {
const userId = parseJwtUserId(req.cookies.jwt)
deleted = await prisma.category.deleteMany({
where: {
id: paramsIdParser.parse(req.params).id,
user_id: req.body.userId,
user_id: userId,
},
})
} catch (error: any) {
+1 -1
View File
@@ -11,6 +11,6 @@ const getAll: RequestHandler = async (req, res) => {
}
frontRouter.use(express.static("snippets-manager-front/dist"))
frontRouter.get("/*", getAll)
frontRouter.get("/", getAll)
export default frontRouter
+9 -1
View File
@@ -67,14 +67,21 @@ export const userRouterPostLogin: RequestHandler = async (req, res) => {
return
}
const jwtToken = jwt.sign({ userId: user?.id }, getJwtSecret(), { expiresIn: "1h" })
const jwtToken = jwt.sign({ userId: user?.id }, getJwtSecret(), { expiresIn: "2h" })
res.cookie("jwt", jwtToken, {
httpOnly: true,
secure: true,
expires: new Date(new Date().getTime() + 7200000),
sameSite: "strict",
}).json({ message: "Logged in! httpOnly cookie set." })
}
export const userRouterPostLogout: RequestHandler = async (req, res) => {
res.clearCookie("jwt", { httpOnly: true, secure: true, sameSite: "strict" }).json({
message: "httpOnly cookie removed.",
})
}
export const userRouterPostRegister: RequestHandler = async (req, res) => {
const loginData = parseLoginData(req.body)
if (loginData == undefined) {
@@ -95,6 +102,7 @@ export const userRouterPostRegister: RequestHandler = async (req, res) => {
}
sessionRouter.post("/login", userRouterPostLogin)
sessionRouter.post("/logout", userRouterPostLogout)
sessionRouter.post("/register", userRouterPostRegister)
export default sessionRouter