From 6bc2b081f1f625ef63dbbb00a37b4de83dbc25c0 Mon Sep 17 00:00:00 2001 From: Lee-Dongwook Date: Sun, 20 Oct 2024 11:50:59 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20=EC=84=9C=EB=B2=84=20=ED=86=A0?= =?UTF-8?q?=ED=81=B0=20=EA=B2=80=EC=A6=9D=20=EB=B0=8F=20=EB=9D=BC=EC=9A=B0?= =?UTF-8?q?=ED=84=B0=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/package.json | 1 + server/secret-generator.js | 4 + .../{authMiddleware.ts => verifyToken.ts} | 7 +- server/src/models/user.model.ts | 12 +- server/src/routes/authRoutes.ts | 15 +- server/src/routes/documentRoutes.ts | 191 ++++++++++-------- 6 files changed, 136 insertions(+), 94 deletions(-) create mode 100644 server/secret-generator.js rename server/src/middleware/{authMiddleware.ts => verifyToken.ts} (74%) diff --git a/server/package.json b/server/package.json index 02cbea1..e1ba15e 100644 --- a/server/package.json +++ b/server/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "description": "", "main": "index.js", + "type":"module", "scripts": { "dev": "nodemon src/server.ts", "test": "jest", diff --git a/server/secret-generator.js b/server/secret-generator.js new file mode 100644 index 0000000..9d0b73b --- /dev/null +++ b/server/secret-generator.js @@ -0,0 +1,4 @@ +import crypto from "crypto"; + +const secret = crypto.randomBytes(32).toString("hex"); +console.log(secret); diff --git a/server/src/middleware/authMiddleware.ts b/server/src/middleware/verifyToken.ts similarity index 74% rename from server/src/middleware/authMiddleware.ts rename to server/src/middleware/verifyToken.ts index e999da3..ea8e353 100644 --- a/server/src/middleware/authMiddleware.ts +++ b/server/src/middleware/verifyToken.ts @@ -5,11 +5,12 @@ export const verifyToken = ( req: Request, res: Response, next: NextFunction -) => { +): void => { const token = req.headers.authorization?.split(" ")[1]; if (!token) { - return res.status(401).json({ message: "No token provided" }); + res.status(401).json({ message: "No token provided. Access denied." }); + return; } try { @@ -20,6 +21,6 @@ export const verifyToken = ( req.body.userId = (decoded as any).userId; next(); } catch (error) { - res.status(401).json({ message: "Invalid token" }); + res.status(400).json({ message: "Invalid token" }); } }; diff --git a/server/src/models/user.model.ts b/server/src/models/user.model.ts index 5c44a01..cbf7a10 100644 --- a/server/src/models/user.model.ts +++ b/server/src/models/user.model.ts @@ -31,11 +31,15 @@ const UserSchema = new Schema({ }); UserSchema.pre("save", async function (next) { - if (this.isModified("password")) { - const salt = await bcrypt.genSalt(10); - this.password = await bcrypt.hash(this.password, salt); + try { + if (this.isModified("password")) { + const salt = await bcrypt.genSalt(10); + this.password = await bcrypt.hash(this.password, salt); + } + next(); + } catch (error: any) { + next(error); } - next(); }); UserSchema.methods.comparePassword = async function ( diff --git a/server/src/routes/authRoutes.ts b/server/src/routes/authRoutes.ts index c8e9571..fdcc0d7 100644 --- a/server/src/routes/authRoutes.ts +++ b/server/src/routes/authRoutes.ts @@ -1,17 +1,18 @@ -import { Router } from "express"; +import { Router, type Response, type Request } from "express"; import bcrypt from "bcrypt"; import jwt from "jsonwebtoken"; import UserModel from "../models/user.model"; const router = Router(); -router.post("/register", async (req, res) => { +router.post("/register", async (req: Request, res: Response): Promise => { const { username, email, password } = req.body; try { const existingUser = await UserModel.findOne({ email }); if (existingUser) { - return res.status(400).json({ message: "Email already existed" }); + res.status(400).json({ message: "Email already existed" }); + return; } const newUser = new UserModel({ username, email, password }); @@ -21,18 +22,20 @@ router.post("/register", async (req, res) => { } }); -router.post("/login", async (req, res) => { +router.post("/login", async (req: Request, res: Response): Promise => { const { email, password } = req.body; try { const user = await UserModel.findOne({ email }); if (!user) { - return res.status(400).json({ message: "Invalid credentials" }); + res.status(400).json({ message: "Invalid credentials" }); + return; } const isMatch = await bcrypt.compare(password, user.password); if (!isMatch) { - return res.status(400).json({ message: "Invalid credentials" }); + res.status(400).json({ message: "Invalid credentials" }); + return; } const token = jwt.sign( diff --git a/server/src/routes/documentRoutes.ts b/server/src/routes/documentRoutes.ts index 2168d30..d7bca07 100644 --- a/server/src/routes/documentRoutes.ts +++ b/server/src/routes/documentRoutes.ts @@ -1,98 +1,127 @@ -import { Router } from "express"; +import { Router, type Response, type Request } from "express"; import DocumentModel from "../models/document.model"; -import { verifyToken } from "../middleware/authMiddleware"; +import { verifyToken } from "../middleware/verifyToken"; const router = Router(); -router.post("/", verifyToken, async (req, res) => { - const { title, content } = req.body; - - try { - const newDocument = new DocumentModel({ title, content }); - await newDocument.save(); - } catch (error) { - res.status(500).json({ message: "Failed to create document", error }); +router.post( + "/", + verifyToken, + async (req: Request, res: Response): Promise => { + const { title, content } = req.body; + + try { + const newDocument = new DocumentModel({ title, content }); + await newDocument.save(); + } catch (error) { + res.status(500).json({ message: "Failed to create document", error }); + } } -}); - -router.get("/:id", verifyToken, async (req, res) => { - try { - const document = await DocumentModel.findById(req.params.id); - if (!document) { - return res.status(404).json({ message: "Document not found" }); +); + +router.get( + "/:id", + verifyToken, + async (req: Request, res: Response): Promise => { + try { + const document = await DocumentModel.findById(req.params.id); + if (!document) { + res.status(404).json({ message: "Document not found" }); + return; + } + + res.json(document); + } catch (error) { + res.status(500).json({ message: "Failed to get document", error }); } - - res.json(document); - } catch (error) { - res.status(500).json({ message: "Failed to get document", error }); } -}); - -router.put("/:id", verifyToken, async (req, res) => { - const { content } = req.body; - try { - const document = await DocumentModel.findByIdAndUpdate( - req.params.id, - { content, $push: { versions: { content } } }, - { new: true } - ); - - if (!document) { - return res.status(404).json({ message: "Document not found" }); +); + +router.put( + "/:id", + verifyToken, + async (req: Request, res: Response): Promise => { + const { content } = req.body; + try { + const document = await DocumentModel.findByIdAndUpdate( + req.params.id, + { content, $push: { versions: { content } } }, + { new: true } + ); + + if (!document) { + res.status(404).json({ message: "Document not found" }); + return; + } + + res.json(document); + } catch (error) { + res.status(500).json({ message: "Failed to update document", error }); } - - res.json(document); - } catch (error) { - res.status(500).json({ message: "Failed to update document", error }); } -}); - -router.delete("/:id", verifyToken, async (req, res) => { - try { - const document = await DocumentModel.findByIdAndDelete(req.params.id); - if (!document) { - return res.status(404).json({ message: "Document not found" }); +); + +router.delete( + "/:id", + verifyToken, + async (req: Request, res: Response): Promise => { + try { + const document = await DocumentModel.findByIdAndDelete(req.params.id); + if (!document) { + res.status(404).json({ message: "Document not found" }); + return; + } + + res.json({ message: "Document deleted successfully" }); + } catch (error) { + res.status(500).json({ message: "Failed to delete document", error }); } - - res.json({ message: "Document deleted successfully" }); - } catch (error) { - res.status(500).json({ message: "Failed to delete document", error }); } -}); - -router.get("/:id/versions", verifyToken, async (req, res) => { - try { - const document = await DocumentModel.findById(req.params.id); - if (!document) { - return res.status(404).json({ message: "Document not found" }); +); + +router.get( + "/:id/versions", + verifyToken, + async (req: Request, res: Response): Promise => { + try { + const document = await DocumentModel.findById(req.params.id); + if (!document) { + res.status(404).json({ message: "Document not found" }); + return; + } + + res.json(document.versions); + } catch (error) { + res.status(500).json({ message: "Failed to fetch versions", error }); } - - res.json(document.versions); - } catch (error) { - res.status(500).json({ message: "Failed to fetch versions", error }); } -}); - -router.put("/:id/restore", verifyToken, async (req, res) => { - const { versionIndex } = req.body; - - try { - const document = await DocumentModel.findByIdAndUpdate(req.params.id); - if ( - !document || - versionIndex < 0 || - versionIndex >= document.versions.length - ) { - return res.status(400).json({ message: "Invalid version index" }); +); + +router.put( + "/:id/restore", + verifyToken, + async (req: Request, res: Response): Promise => { + const { versionIndex } = req.body; + + try { + const document = await DocumentModel.findByIdAndUpdate(req.params.id); + if ( + !document || + versionIndex < 0 || + versionIndex >= document.versions.length + ) { + res.status(400).json({ message: "Invalid version index" }); + return; + } + + document.content = document.versions[versionIndex].content; + await document.save(); + + res.json(document); + } catch (error) { + res.status(500).json({ message: "Failed to restore version:", error }); } - - document.content = document.versions[versionIndex].content; - await document.save(); - - res.json(document); - } catch (error) { - res.status(500).json({ message: "Failed to restore version:", error }); } -}); +); export default router;