mirror of
https://github.com/Floriansylvain/SnippetsManager.git
synced 2026-08-19 11:43:15 +02:00
fix: userId from req.body wtf
This commit is contained in:
+1
-1
Submodule snippets-manager-front updated: f8a75e9cdd...ec9bc1bb4b
+15
-11
@@ -2,7 +2,7 @@ import { Category, Prisma, PrismaClient } from "@prisma/client"
|
|||||||
import express, { RequestHandler } from "express"
|
import express, { RequestHandler } from "express"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { getPaginationLinks, Pagination, queryPaginationParser } from "../utils/pagination.js"
|
import { getPaginationLinks, Pagination, queryPaginationParser } from "../utils/pagination.js"
|
||||||
import { userIdMiddleware } from "./user.js"
|
import { parseJwtUserId, userIdMiddleware } from "./user.js"
|
||||||
|
|
||||||
const categoryRouter = express.Router()
|
const categoryRouter = express.Router()
|
||||||
const prisma = new PrismaClient()
|
const prisma = new PrismaClient()
|
||||||
@@ -37,7 +37,8 @@ const categoryGet: RequestHandler = async (req, res) => {
|
|||||||
const pagination = queryPaginationParser.parse(req.query)
|
const pagination = queryPaginationParser.parse(req.query)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
categories = await findCategories(req.body.userId, pagination)
|
const userId = parseJwtUserId(req.cookies.jwt)
|
||||||
|
categories = await findCategories(userId ?? -1, pagination)
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
res.status(400).json({ message: error.issues ?? error })
|
res.status(400).json({ message: error.issues ?? error })
|
||||||
return
|
return
|
||||||
@@ -55,10 +56,11 @@ const categoryGetUnique: RequestHandler = async (req, res) => {
|
|||||||
let category: Category | null = null
|
let category: Category | null = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const userId = parseJwtUserId(req.cookies.jwt)
|
||||||
category = await prisma.category.findFirst({
|
category = await prisma.category.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id: paramsIdParser.parse(req.params).id,
|
id: paramsIdParser.parse(req.params).id,
|
||||||
user_id: req.body.userId,
|
user_id: userId,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@@ -71,19 +73,19 @@ const categoryGetUnique: RequestHandler = async (req, res) => {
|
|||||||
|
|
||||||
const categoryPost: RequestHandler = async (req, res) => {
|
const categoryPost: RequestHandler = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const newCategory = categoryPostParser.parse(req.body)
|
const userId = parseJwtUserId(req.cookies.jwt)
|
||||||
await prisma.category.create({
|
const parsedCategory = categoryPostParser.parse(req.body)
|
||||||
|
const newCategorey = await prisma.category.create({
|
||||||
data: {
|
data: {
|
||||||
name: newCategory.name,
|
name: parsedCategory.name,
|
||||||
user_id: req.body.userId,
|
user_id: userId ?? -1,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
res.json({ message: "Category successfully added.", id: newCategorey.id })
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
res.status(400).json({ message: error.issues ?? error })
|
res.status(400).json({ message: error.issues ?? error })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({ message: "Category successfully added." })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const categoryUpdate: RequestHandler = async (req, res) => {
|
const categoryUpdate: RequestHandler = async (req, res) => {
|
||||||
@@ -91,10 +93,11 @@ const categoryUpdate: RequestHandler = async (req, res) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const categoryToUpdate = categoryUpdateParser.parse(req.body)
|
const categoryToUpdate = categoryUpdateParser.parse(req.body)
|
||||||
|
const userId = parseJwtUserId(req.cookies.jwt)
|
||||||
updated = await prisma.category.updateMany({
|
updated = await prisma.category.updateMany({
|
||||||
where: {
|
where: {
|
||||||
id: paramsIdParser.parse(req.params).id,
|
id: paramsIdParser.parse(req.params).id,
|
||||||
user_id: req.body.userId,
|
user_id: userId,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
name: categoryToUpdate.name,
|
name: categoryToUpdate.name,
|
||||||
@@ -112,10 +115,11 @@ const categoryDelete: RequestHandler = async (req, res) => {
|
|||||||
let deleted: Prisma.BatchPayload
|
let deleted: Prisma.BatchPayload
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const userId = parseJwtUserId(req.cookies.jwt)
|
||||||
deleted = await prisma.category.deleteMany({
|
deleted = await prisma.category.deleteMany({
|
||||||
where: {
|
where: {
|
||||||
id: paramsIdParser.parse(req.params).id,
|
id: paramsIdParser.parse(req.params).id,
|
||||||
user_id: req.body.userId,
|
user_id: userId,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
|||||||
@@ -11,6 +11,6 @@ const getAll: RequestHandler = async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
frontRouter.use(express.static("snippets-manager-front/dist"))
|
frontRouter.use(express.static("snippets-manager-front/dist"))
|
||||||
frontRouter.get("/*", getAll)
|
frontRouter.get("/", getAll)
|
||||||
|
|
||||||
export default frontRouter
|
export default frontRouter
|
||||||
|
|||||||
@@ -67,14 +67,21 @@ export const userRouterPostLogin: RequestHandler = async (req, res) => {
|
|||||||
return
|
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, {
|
res.cookie("jwt", jwtToken, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: true,
|
secure: true,
|
||||||
|
expires: new Date(new Date().getTime() + 7200000),
|
||||||
sameSite: "strict",
|
sameSite: "strict",
|
||||||
}).json({ message: "Logged in! httpOnly cookie set." })
|
}).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) => {
|
export const userRouterPostRegister: RequestHandler = async (req, res) => {
|
||||||
const loginData = parseLoginData(req.body)
|
const loginData = parseLoginData(req.body)
|
||||||
if (loginData == undefined) {
|
if (loginData == undefined) {
|
||||||
@@ -95,6 +102,7 @@ export const userRouterPostRegister: RequestHandler = async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sessionRouter.post("/login", userRouterPostLogin)
|
sessionRouter.post("/login", userRouterPostLogin)
|
||||||
|
sessionRouter.post("/logout", userRouterPostLogout)
|
||||||
sessionRouter.post("/register", userRouterPostRegister)
|
sessionRouter.post("/register", userRouterPostRegister)
|
||||||
|
|
||||||
export default sessionRouter
|
export default sessionRouter
|
||||||
|
|||||||
Reference in New Issue
Block a user