From 3f7e29d23999e78f7d0125f0d35ff4da491cf5da Mon Sep 17 00:00:00 2001 From: Won Chan Lee Date: Fri, 19 Jan 2024 15:42:29 +0900 Subject: [PATCH 01/10] =?UTF-8?q?User=20phone=20column=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=ED=9B=84=20=ED=9A=8C=EC=9B=90=20=EC=A0=80=EC=9E=A5,?= =?UTF-8?q?=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/user/controller.js | 134 +++++++++++++++++++++++++++---------- src/api/user/repogitory.js | 10 +-- src/router.js | 28 +++----- 3 files changed, 114 insertions(+), 58 deletions(-) diff --git a/src/api/user/controller.js b/src/api/user/controller.js index c56959a..04ff92f 100644 --- a/src/api/user/controller.js +++ b/src/api/user/controller.js @@ -1,43 +1,107 @@ const jwt = require("./jwt"); -const { register, find } = require("./repogitory"); +const userRepository = require("./repogitory"); const crypto = require("crypto"); exports.register = async (req, res) => { - const { email, password, name } = req.body; - let { count } = await find(email); - if (typeof password !== "string" || !password.trim()) { - return res.status(400).send({ - isSuccess: "false", - message: "비밀번호는 필수 입력 항목입니다.", - }); - } - if (count > 0) { - return res.send({ - isSuccess: "false", - message: "중복된 이메일이 존재합니다.", - }); - } - const result = await crypto.pbkdf2Sync( - password, - process.env.SALT_KET, - 50, - 100, - "sha512" - ); - - const { affectedRows, insertId } = await register( - email, - result.toString("base64"), - name - ); - if (affectedRows > 0) { - const data = await jwt.jwtSign({ id: insertId, name }); - res.send({ access_token: data }); - } else { + // 영어, 숫자 최소 1자리 + const emailRegex = /^[a-zA-Z0-9]+$/; + // 영어, 숫자 최소 1자리 + const passwordRegex = /^[a-zA-Z0-9]+$/; + // 한글, 영어, 숫자 최소 1자리 + const nameRegex = /^[가-힣a-zA-Z0-9]+$/; + // 숫자 최소 1자리 + const phoneRegex = /^[0-9]+$/; + + const {email, password, name, phone} = req.body; + + if ( + emailRegex.test(email) === false || + passwordRegex.test(password) === false || + nameRegex.test(name) === false || + phoneRegex.test(phone) === false + ) { + return res.send({ + isSuccess: false, + message: "email, password, name, phone 은 숫자와 영어만 입력 가능합니다.", + }); + } + + let isExist = await userRepository.isExistByEmail(email); + + if (isExist) { + return res.send({ + isSuccess: false, + message: "중복된 이메일이 존재합니다.", + }); + } + + // 비밀번호 암호화 + const result = await crypto.pbkdf2Sync( + password, + process.env.SALT_KET, + 50, + 100, + "sha512" + ); + + const {affectedRows, insertId} = await userRepository.register( + email, + result.toString("base64"), + name, + phone + ); + + const data = await jwt.jwtSign({id: insertId}); + res.send({ - isSuccess: "false", - message: "항목 중 빈 칸이 존재하거나 올바른 값이 아닙니다.", + isSuccess: true, + userId : insertId, + access_token: data, }); - } }; + +exports.login = async (req, res) => { + let {email, password} = req.body; + + // 영어, 숫자 최소 1자리 + const emailRegex = /^[a-zA-Z0-9]+$/; + // 영어, 숫자 최소 1자리 + const passwordRegex = /^[a-zA-Z0-9]+$/; + + if ( + emailRegex.test(email) === false || + passwordRegex.test(password) === false + ) { + return res.send({ + isSuccess: false, + message: "email, password 는 숫자와 영어만 입력 가능합니다.", + }); + } + + // 비밀번호 암호화 + const result = await crypto.pbkdf2Sync( + password, + process.env.SALT_KET, + 50, + 100, + "sha512" + ); + + const user = await userRepository.login(email, result.toString("base64")); + + if (!user){ + return res.send({ + isSuccess: false, + message: "일치하는 회원이 없습니다.", + }); + } + + const token = await jwt.jwtSign({id: user.id}); + + res.send({ + isSuccess: true, + userId : user.id, + access_token: token, + }); +} diff --git a/src/api/user/repogitory.js b/src/api/user/repogitory.js index 6777ecc..825d396 100644 --- a/src/api/user/repogitory.js +++ b/src/api/user/repogitory.js @@ -1,8 +1,8 @@ const { pool } = require("../../data"); -exports.register = async (email, password, name) => { - const query = `INSERT INTO user (email, password, name) VALUES (?,?,?)`; - return await pool(query, [email, password, name]); +exports.register = async (email, password, name, phone) => { + const query = `INSERT INTO user (email, password, name, phone) VALUES (?,?,?,?)`; + return await pool(query, [email, password, name, phone]); }; exports.login = async (email, password) => { @@ -11,11 +11,11 @@ exports.login = async (email, password) => { return result.length < 0 ? null : result[0]; }; -exports.find = async (email) => { +exports.isExistByEmail = async (email) => { let result = await pool(`SELECT count(*) count FROM user WHERE email = ?`, [ email, ]); - return result.length < 0 ? null : result[0]; + return result.length >= 0; }; exports.show_user = async (id) => { diff --git a/src/router.js b/src/router.js index 9c775ea..07f6f6a 100644 --- a/src/router.js +++ b/src/router.js @@ -18,31 +18,23 @@ const storage = multer.diskStorage({ const upload = multer({ storage: storage }); -const webController = require("./web/controller"); -const apiUserController = require("./api/user/controller"); -const apiFeedCOntroller = require("./api/feed/controller"); -const fileController = require("./api/file/controller"); +const userController = require("./api/user/controller"); const cardsController = require("./api/cards/controller"); - const walletController = require("./api/wallet/controller"); + +// static file router.use("/", express.static("./public")); +// CORS setting router.use(headers); +// logging router.use(logging); -router.post("/api/file", upload.single("file"), fileController.upload); -router.get("/api/file/:id", fileController.download); - -router.get("/", webController.home); -router.get("/page/:page", webController.page); -router.get("/sitemap", webController.sitemap); - -router.post("/api/user/register", apiUserController.register); -router.get("/api/feed", verify, apiFeedCOntroller.index); -router.post("/api/feed", verify, apiFeedCOntroller.store); -router.get("/api/feed/:id", verify, apiFeedCOntroller.show); -router.post("/api/feed/:id", verify, apiFeedCOntroller.update); -router.post("/api/feed/:id/delete", verify, apiFeedCOntroller.destroy); +// 유저 관련 api +// 회원가입 +router.post("/api/users/register", userController.register); +// 로그인 +router.post("/api/users/login", userController.login); //명함 관련 api router.post( From bf9f080e06312f081824b7e76bcef054b9fe10fa Mon Sep 17 00:00:00 2001 From: Won Chan Lee Date: Fri, 19 Jan 2024 15:46:23 +0900 Subject: [PATCH 02/10] =?UTF-8?q?=ED=86=A0=ED=81=B0=20=EC=9D=B8=EC=A6=9D?= =?UTF-8?q?=EC=8B=9C=20=ED=86=A0=ED=81=B0=20=EC=98=88=EC=99=B8=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/middleware/jwtVerify.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/middleware/jwtVerify.js b/src/middleware/jwtVerify.js index 0eb7b82..2d6c022 100644 --- a/src/middleware/jwtVerify.js +++ b/src/middleware/jwtVerify.js @@ -5,7 +5,11 @@ module.exports = async (req, res, next) => { jwt.verify(token, process.env.JWT_KEY, function (err, decoded) { if (err) { - return res.send(err); + return res.send({ + isSuccess: false, + message: "토큰이 유효하지 않습니다.", + err + }); } req.user = decoded; next(); From 52131f34eb8bce0bb3a39cf450b290f831576b59 Mon Sep 17 00:00:00 2001 From: Won Chan Lee Date: Fri, 19 Jan 2024 15:59:19 +0900 Subject: [PATCH 03/10] =?UTF-8?q?=ED=9A=8C=EC=9B=90=20=ED=83=88=ED=87=B4?= =?UTF-8?q?=EC=8B=9C=20isActivate=20column=20false=EB=90=98=EA=B2=8C=20?= =?UTF-8?q?=ED=9A=8C=EC=9B=90=20=ED=83=88=ED=87=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/user/controller.js | 44 ++++++++++++++++++++++++++++++++++++++ src/api/user/repogitory.js | 4 ++++ src/router.js | 2 ++ 3 files changed, 50 insertions(+) diff --git a/src/api/user/controller.js b/src/api/user/controller.js index 04ff92f..9ee33cb 100644 --- a/src/api/user/controller.js +++ b/src/api/user/controller.js @@ -105,3 +105,47 @@ exports.login = async (req, res) => { access_token: token, }); } + +exports.delete = async (req, res) => { + let {userId} = req.body + const tokenUserId = req.user.id; + + + // 숫자 최소 1자리 + const regExp = /^[0-9]+$/; + + if (!regExp.test(userId)) { + return res.send({ + isSuccess: false, + message: "userId가 잘못되었습니다.", + }); + } + + userId = Number(userId); + + // 토큰 유저 아이디와 요청 유저 아이디가 다를 경우 + if (tokenUserId !== userId) { + return res.send({ + isSuccess: false, + message: "인증실패 : 토큰 유저 아이디와 요청 유저 아이디가 다릅니다.", + }); + } + + // 유저 아이디로 유저 찾기 + const user = await userRepository.show_user(userId); + if (!user) { + return res.send({ + isSuccess: false, + message: "존재하지 않는 유저입니다.", + }); + } + + let {affectedRows} = await userRepository.delete(userId); + + if (affectedRows > 0) { + res.send({ isSuccess: true }); + } + else { + res.send({ isSuccess: false, message: "삭제 실패" }); + } +} \ No newline at end of file diff --git a/src/api/user/repogitory.js b/src/api/user/repogitory.js index 825d396..4ee5c4d 100644 --- a/src/api/user/repogitory.js +++ b/src/api/user/repogitory.js @@ -18,6 +18,10 @@ exports.isExistByEmail = async (email) => { return result.length >= 0; }; +exports.delete = async (userId) => { + return await pool(`UPDATE user SET isActivated = false WHERE id = ?`, [userId]); +} + exports.show_user = async (id) => { const query = `SELECT * FROM user WHERE id =?`; let result = await pool(query, [id]); diff --git a/src/router.js b/src/router.js index 07f6f6a..c07e36d 100644 --- a/src/router.js +++ b/src/router.js @@ -35,6 +35,8 @@ router.use(logging); router.post("/api/users/register", userController.register); // 로그인 router.post("/api/users/login", userController.login); +// 회원탈퇴 +router.post("/api/users/delete",verify, userController.delete); //명함 관련 api router.post( From 8a42a6e2269bc7d0f2a37e2cf59c9b86af268e1c Mon Sep 17 00:00:00 2001 From: ohamin26 Date: Fri, 19 Jan 2024 16:34:34 +0900 Subject: [PATCH 04/10] =?UTF-8?q?=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 --- src/api/cards/controller.js | 125 +++++++++++++++++++----------------- src/api/cards/repogitory.js | 17 ++--- 2 files changed, 76 insertions(+), 66 deletions(-) diff --git a/src/api/cards/controller.js b/src/api/cards/controller.js index fb88c23..9496efd 100644 --- a/src/api/cards/controller.js +++ b/src/api/cards/controller.js @@ -1,14 +1,19 @@ const repository = require("./repogitory"); -const userRepogitory = require("../user/repogitory"); +const userRepository = require("../user/repogitory"); const jwt = require("jsonwebtoken"); //내 명함 정보 등록 exports.register = async (req, res) => { - const { user_id, position, organization, address, tell, email } = req.body; + let { userId, position, organization, address, tell, email } = req.body; const file = req.files; - // 확장자까지 넣기 - // 사진 경로 받기 + if (!userId || !position || !organization || !address || !tell || !email) { + return res.send({ + isSuccess: false, + message: "항목 중 null 값이 존재합니다.", + }); + } + let time = new Date(); const photo = "http://" + @@ -17,16 +22,14 @@ exports.register = async (req, res) => { file["file"][0].filename + time.getTime(); - // jwt 토큰 값 받고 id 값만 분리하기 - db에 user_id에 저장하기 위함 + // jwt 토큰 값 받고 id 값만 분리하기 - db에 userId에 저장하기 위함 const { access_token } = req.headers; //const [tokenType, tokenValue] = authorization.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - - checkUserInfo(res, user_id, id); - + checkUserInfo(res, userId, id); //db에 저장 정상적으로 저장 시 ok / 실패 시 fail const { affectedRows, insertId } = await repository.create( - (user_id = id), + (userId = id), position, organization, address, @@ -35,34 +38,43 @@ exports.register = async (req, res) => { email ); if (affectedRows > 0) { - return res.send({ isSuccess: "true", id: insertId }); + return res.send({ isSuccess: true, id: insertId }); } - return res.send({ isSuccess: "false", message: "등록 실패" }); + return res.send({ isSuccess: false, message: "등록 실패" }); }; //내 명함 정보 조회 exports.inquiry = async (req, res) => { const cardId = req.params.cardId; - //user_id 가져오기 + //userId 가져오기 const { access_token } = req.headers; //const [tokenType, tokenValue] = authorization.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - const { user_id } = req.body; + let { userId } = req.body; - checkUserInfo(res, user_id, id); + checkUserInfo(res, userId, id); // 명함 정보 가져오기 const item = await repository.show({ cardId, userId: id }); if (item === null) { return res.send({ - isSuccess: "false", + isSuccess: false, message: "조회된 값이 없습니다(cardId나 userId를 확인해주세요)", }); } + //유저 정보 가져오기 + const user_info = await userRepository.show_user(item.userId); + if (user_info == null) { + return res.send({ + isSuccess: false, + message: "조회된 값이 없습니다(userId를 확인해주세요)", + }); + } + const response = { - isSuccess: "true", + isSuccess: true, position: item.position, organization: item.organization, address: item.address, @@ -78,19 +90,18 @@ exports.inquiry = async (req, res) => { //내 명함 정보 전체 조회 exports.inquiry_all = async (req, res) => { - //user_id 값 가져오기 + //userId 값 가져오기 const { access_token } = req.headers; - const { user_id } = req.body; + let { userId } = req.body; //const [tokenType, tokenValue] = access_token.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - - checkUserInfo(res, user_id, id); + checkUserInfo(res, userId, id); const item_all = await repository.show_all(id); res.send({ - isSuccess: "true", - resutl: item_all, + isSuccess: true, + result: item_all, }); }; @@ -102,21 +113,21 @@ exports.inquiry_other = async (req, res) => { const item = await repository.show_other(cardId); if (item == null) { res.send({ - isSuccess: "false", + isSuccess: false, message: "조회된 값이 없습니다(cardId를 확인해주세요)", }); } //유저 정보 가져오기 - const user_info = await userRepogitory.show_user(item.user_id); + const user_info = await userRepository.show_user(item.userId); if (user_info == null) { return res.send({ - isSuccess: "false", - message: "조회된 값이 없습니다(user_Id를 확인해주세요)", + isSuccess: false, + message: "조회된 값이 없습니다(userId를 확인해주세요)", }); } const response = { - isSuccess: "true", + isSuccess: true, position: item.position, organization: item.organization, address: item.address, @@ -137,26 +148,28 @@ exports.update = async (req, res) => { const { access_token } = req.headers; //const [tokenType, tokenValue] = access_token.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - let { user_id, position, organization, address, photo, tell, email } = + let { userId, position, organization, address, photo, tell, email } = req.body; - checkUserInfo(res, user_id, id); + checkUserInfo(res, userId, id); //명함 정보 가져오기 - const item = await repository.show(cardId); + const item = await repository.show({ cardId, userId }); if (item == null) { res.send({ - isSuccess: "false", - message: "조회된 값이 없습니다(cardId를 확인해주세요)", + isSuccess: false, + message: "조회된 값이 없습니다(cardId 또는 userId를 확인해주세요)", }); } - //수정된 항목만 업데이트 - position = position || item.position; - organization = organization || item.organization; - address = address || item.address; - photo = photo || item.photo; - tell = tell || item.tell; - email = email || item.email; + //수정된 항목만 업데이트 , body 항목의 null 값 검증 + position ? (position = position) : (position = item.position); + organization + ? (organization = organization) + : (organization = item.organization); + address ? (address = address) : (address = item.address); + photo ? (photo = photo) : (photo = item.photo); + tell ? (tell = tell) : (tell = item.tell); + email ? (email = email) : (email = item.email); const { affectedRows, insertId } = await repository.update( cardId, @@ -169,9 +182,9 @@ exports.update = async (req, res) => { ); if (affectedRows > 0) { - return res.send({ isSuccess: "true", id: insertId }); + return res.send({ isSuccess: true, id: insertId }); } - return res.send({ isSuccess: "false", message: "저장 실패" }); + return res.send({ isSuccess: false, message: "저장 실패" }); }; //내 명함 목록 삭제 @@ -180,16 +193,15 @@ exports.delete = async (req, res) => { const { access_token } = req.headers; //const [tokenType, tokenValue] = access_token.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - let { user_id } = req.body; - - checkUserInfo(res, user_id, id); + let { userId } = req.body; + checkUserInfo(res, userId, id); const { affectedRows, insertId } = await repository.delete(cardId); - if (affectedRows > 0) { - res.send({ isSuccess: "true" }); + if (affectedRows == 0) { + res.send({ isSuccess: true }); } else { - res.send({ isSuccess: "false", message: "삭제 실패" }); + res.send({ isSuccess: false, message: "삭제 실패" }); } }; @@ -197,28 +209,25 @@ const checkUserInfo = async (res, userId, id) => { // 유저 정보 확인하기 if (userId != id) { return res.send({ - isSuccess: "false", + isSuccess: false, message: "올바른 토큰 값이 아닙니다.", }); } - // user_id, id 타입 일치 확인 - if (typeof userId !== typeof id) { + // userId, id 타입 일치 확인 + if (userId !== id) { return res.send({ - isSuccess: "false", - message: "타입이 일치하지 않습니다.(user_id 타입은 int형 입니다.)", + isSuccess: false, + message: "타입이 일치하지 않습니다.(userId 타입은 int형 입니다.)", }); } // 유저 정보 가져오기 - const user_info = await userRepogitory.show_user(item.user_id); + const user_info = await userRepository.show_user(userId); if (user_info === null) { return res.send({ - isSuccess: "false", - message: `조회된 값이 없습니다(${errorMessage})`, + isSuccess: false, + message: `조회된 값이 없습니다`, }); } - - // 함수를 통과했다면, 유효한 정보 반환 - return { item, user_info }; }; diff --git a/src/api/cards/repogitory.js b/src/api/cards/repogitory.js index 7386e38..d732d59 100644 --- a/src/api/cards/repogitory.js +++ b/src/api/cards/repogitory.js @@ -2,7 +2,7 @@ const { pool } = require("../../data"); //내 명함 등록 쿼리 exports.create = async ( - user_id, + userId, position, organization, address, @@ -10,9 +10,9 @@ exports.create = async ( tell, email ) => { - const query = `INSERT INTO cards (user_id, position, organization, address, photo, tell, email) VALUES (?,?,?,?,?,?,?);`; + const query = `INSERT INTO cards (userId, position, organization, address, photo, tell, email) VALUES (?,?,?,?,?,?,?);`; return await pool(query, [ - user_id, + userId, position, organization, address, @@ -24,7 +24,8 @@ exports.create = async ( //내 명함 조회 쿼리 exports.show = async ({ cardId, userId }) => { - const query = `SELECT * FROM cards WHERE card_id=? AND user_id =?`; + console.log(cardId, userId); + const query = `SELECT * FROM cards WHERE cardId=? AND userId =?`; let result = await pool(query, [cardId, userId]); return result.length < 0 ? null : result[0]; }; @@ -32,14 +33,14 @@ exports.show = async ({ cardId, userId }) => { //내 명함 전체 조회 exports.show_all = async (id) => { const query = ` - SELECT cards.*, user.phone, user.email, user.name FROM cards JOIN user ON cards.user_id = user.id WHERE user_id=?`; + SELECT cards.*, user.phone, user.email, user.name FROM cards JOIN user ON cards.userId = user.id WHERE userId=?`; const result = await pool(query, [id]); return result.length < 0 ? null : result; }; //다른 명함 조회 쿼리 exports.show_other = async (id) => { - const query = `SELECT * FROM cards WHERE card_id =?`; + const query = `SELECT * FROM cards WHERE cardId =?`; let result = await pool(query, [id]); return result.length < 0 ? null : result[0]; }; @@ -57,7 +58,7 @@ exports.update = async ( const query = ` UPDATE cards SET position=?, organization=?, address=?, photo=?, tell=?, email=? - WHERE card_id = ?; + WHERE cardId = ?; `; return await pool(query, [ @@ -75,7 +76,7 @@ exports.update = async ( exports.delete = async (id) => { const query = ` DELETE FROM cards - WHERE card_id = ?; + WHERE cardId = ?; `; return await pool(query, [id]); From a71848a8ca8d860ddf1313ddaad4da8fea8cf091 Mon Sep 17 00:00:00 2001 From: Won Chan Lee Date: Fri, 19 Jan 2024 16:49:30 +0900 Subject: [PATCH 05/10] =?UTF-8?q?isExistByEmail=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/user/repogitory.js | 40 +++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/api/user/repogitory.js b/src/api/user/repogitory.js index 4ee5c4d..451b0fd 100644 --- a/src/api/user/repogitory.js +++ b/src/api/user/repogitory.js @@ -1,29 +1,41 @@ -const { pool } = require("../../data"); +const {pool} = require("../../data"); exports.register = async (email, password, name, phone) => { - const query = `INSERT INTO user (email, password, name, phone) VALUES (?,?,?,?)`; - return await pool(query, [email, password, name, phone]); + const query = `INSERT INTO user (email, password, name, phone) + VALUES (?, ?, ?, ?)`; + return await pool(query, [email, password, name, phone]); }; exports.login = async (email, password) => { - const query = `SELECT * FROM user WHERE email = ? AND password = ?`; - let result = await pool(query, [email, password]); - return result.length < 0 ? null : result[0]; + const query = `SELECT * + FROM user + WHERE email = ? + AND password = ?`; + let result = await pool(query, [email, password]); + return result.length < 0 ? null : result[0]; }; exports.isExistByEmail = async (email) => { - let result = await pool(`SELECT count(*) count FROM user WHERE email = ?`, [ - email, - ]); - return result.length >= 0; + let result = await pool( + `SELECT count(*) count + FROM user + WHERE email = ?`, + [email] + ); + + return result[0].count > 0; }; exports.delete = async (userId) => { - return await pool(`UPDATE user SET isActivated = false WHERE id = ?`, [userId]); + return await pool(`UPDATE user + SET isActivated = false + WHERE id = ?`, [userId]); } exports.show_user = async (id) => { - const query = `SELECT * FROM user WHERE id =?`; - let result = await pool(query, [id]); - return result.length < 0 ? null : result[0]; + const query = `SELECT * + FROM user + WHERE id = ?`; + let result = await pool(query, [id]); + return result.length < 0 ? null : result[0]; }; From 55dd58a8eaf47a7556926464553084085c5e23c4 Mon Sep 17 00:00:00 2001 From: Won Chan Lee Date: Fri, 19 Jan 2024 16:49:37 +0900 Subject: [PATCH 06/10] =?UTF-8?q?Dockerfile=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 25 +++++++++++++++++++++++++ Dockerfile | 13 +++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a9bf568 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +# Ignore node modules +node_modules + +# Ignore logs +npm-debug.log +npm-error.log + +# Ignore the Git repository and its logs +.git +.gitignore +.gitattributes + +# Ignore Docker files (optional) +Dockerfile +.dockerignore + +# Ignore environment files +.env +.env.* + +# Ignore other unnecessary files +README.md +LICENSE +.DS_Store +*.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1c82332 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM node:lts + +WORKDIR /app + +COPY package.json /app/package.json + +RUN npm install + +COPY . /app + +EXPOSE 8000 + +CMD ["npm", "start"] \ No newline at end of file From 2e8221b31884f9c20e4370272bb7520adfb26ca7 Mon Sep 17 00:00:00 2001 From: Won Chan Lee Date: Fri, 19 Jan 2024 17:01:03 +0900 Subject: [PATCH 07/10] =?UTF-8?q?User=20Delete=EC=97=90=20isActivate=20?= =?UTF-8?q?=EC=98=A4=ED=83=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/user/repogitory.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/user/repogitory.js b/src/api/user/repogitory.js index 451b0fd..7c6003e 100644 --- a/src/api/user/repogitory.js +++ b/src/api/user/repogitory.js @@ -28,7 +28,7 @@ exports.isExistByEmail = async (email) => { exports.delete = async (userId) => { return await pool(`UPDATE user - SET isActivated = false + SET isActivate = false WHERE id = ?`, [userId]); } From 3c17cff7feab966d2f5ddbec6dd08058ebd8866d Mon Sep 17 00:00:00 2001 From: ohamin26 Date: Fri, 19 Jan 2024 17:23:07 +0900 Subject: [PATCH 08/10] =?UTF-8?q?=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 --- src/api/cards/controller.js | 125 +++++++++++++++++++----------------- src/api/cards/repogitory.js | 17 ++--- 2 files changed, 76 insertions(+), 66 deletions(-) diff --git a/src/api/cards/controller.js b/src/api/cards/controller.js index fb88c23..9496efd 100644 --- a/src/api/cards/controller.js +++ b/src/api/cards/controller.js @@ -1,14 +1,19 @@ const repository = require("./repogitory"); -const userRepogitory = require("../user/repogitory"); +const userRepository = require("../user/repogitory"); const jwt = require("jsonwebtoken"); //내 명함 정보 등록 exports.register = async (req, res) => { - const { user_id, position, organization, address, tell, email } = req.body; + let { userId, position, organization, address, tell, email } = req.body; const file = req.files; - // 확장자까지 넣기 - // 사진 경로 받기 + if (!userId || !position || !organization || !address || !tell || !email) { + return res.send({ + isSuccess: false, + message: "항목 중 null 값이 존재합니다.", + }); + } + let time = new Date(); const photo = "http://" + @@ -17,16 +22,14 @@ exports.register = async (req, res) => { file["file"][0].filename + time.getTime(); - // jwt 토큰 값 받고 id 값만 분리하기 - db에 user_id에 저장하기 위함 + // jwt 토큰 값 받고 id 값만 분리하기 - db에 userId에 저장하기 위함 const { access_token } = req.headers; //const [tokenType, tokenValue] = authorization.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - - checkUserInfo(res, user_id, id); - + checkUserInfo(res, userId, id); //db에 저장 정상적으로 저장 시 ok / 실패 시 fail const { affectedRows, insertId } = await repository.create( - (user_id = id), + (userId = id), position, organization, address, @@ -35,34 +38,43 @@ exports.register = async (req, res) => { email ); if (affectedRows > 0) { - return res.send({ isSuccess: "true", id: insertId }); + return res.send({ isSuccess: true, id: insertId }); } - return res.send({ isSuccess: "false", message: "등록 실패" }); + return res.send({ isSuccess: false, message: "등록 실패" }); }; //내 명함 정보 조회 exports.inquiry = async (req, res) => { const cardId = req.params.cardId; - //user_id 가져오기 + //userId 가져오기 const { access_token } = req.headers; //const [tokenType, tokenValue] = authorization.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - const { user_id } = req.body; + let { userId } = req.body; - checkUserInfo(res, user_id, id); + checkUserInfo(res, userId, id); // 명함 정보 가져오기 const item = await repository.show({ cardId, userId: id }); if (item === null) { return res.send({ - isSuccess: "false", + isSuccess: false, message: "조회된 값이 없습니다(cardId나 userId를 확인해주세요)", }); } + //유저 정보 가져오기 + const user_info = await userRepository.show_user(item.userId); + if (user_info == null) { + return res.send({ + isSuccess: false, + message: "조회된 값이 없습니다(userId를 확인해주세요)", + }); + } + const response = { - isSuccess: "true", + isSuccess: true, position: item.position, organization: item.organization, address: item.address, @@ -78,19 +90,18 @@ exports.inquiry = async (req, res) => { //내 명함 정보 전체 조회 exports.inquiry_all = async (req, res) => { - //user_id 값 가져오기 + //userId 값 가져오기 const { access_token } = req.headers; - const { user_id } = req.body; + let { userId } = req.body; //const [tokenType, tokenValue] = access_token.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - - checkUserInfo(res, user_id, id); + checkUserInfo(res, userId, id); const item_all = await repository.show_all(id); res.send({ - isSuccess: "true", - resutl: item_all, + isSuccess: true, + result: item_all, }); }; @@ -102,21 +113,21 @@ exports.inquiry_other = async (req, res) => { const item = await repository.show_other(cardId); if (item == null) { res.send({ - isSuccess: "false", + isSuccess: false, message: "조회된 값이 없습니다(cardId를 확인해주세요)", }); } //유저 정보 가져오기 - const user_info = await userRepogitory.show_user(item.user_id); + const user_info = await userRepository.show_user(item.userId); if (user_info == null) { return res.send({ - isSuccess: "false", - message: "조회된 값이 없습니다(user_Id를 확인해주세요)", + isSuccess: false, + message: "조회된 값이 없습니다(userId를 확인해주세요)", }); } const response = { - isSuccess: "true", + isSuccess: true, position: item.position, organization: item.organization, address: item.address, @@ -137,26 +148,28 @@ exports.update = async (req, res) => { const { access_token } = req.headers; //const [tokenType, tokenValue] = access_token.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - let { user_id, position, organization, address, photo, tell, email } = + let { userId, position, organization, address, photo, tell, email } = req.body; - checkUserInfo(res, user_id, id); + checkUserInfo(res, userId, id); //명함 정보 가져오기 - const item = await repository.show(cardId); + const item = await repository.show({ cardId, userId }); if (item == null) { res.send({ - isSuccess: "false", - message: "조회된 값이 없습니다(cardId를 확인해주세요)", + isSuccess: false, + message: "조회된 값이 없습니다(cardId 또는 userId를 확인해주세요)", }); } - //수정된 항목만 업데이트 - position = position || item.position; - organization = organization || item.organization; - address = address || item.address; - photo = photo || item.photo; - tell = tell || item.tell; - email = email || item.email; + //수정된 항목만 업데이트 , body 항목의 null 값 검증 + position ? (position = position) : (position = item.position); + organization + ? (organization = organization) + : (organization = item.organization); + address ? (address = address) : (address = item.address); + photo ? (photo = photo) : (photo = item.photo); + tell ? (tell = tell) : (tell = item.tell); + email ? (email = email) : (email = item.email); const { affectedRows, insertId } = await repository.update( cardId, @@ -169,9 +182,9 @@ exports.update = async (req, res) => { ); if (affectedRows > 0) { - return res.send({ isSuccess: "true", id: insertId }); + return res.send({ isSuccess: true, id: insertId }); } - return res.send({ isSuccess: "false", message: "저장 실패" }); + return res.send({ isSuccess: false, message: "저장 실패" }); }; //내 명함 목록 삭제 @@ -180,16 +193,15 @@ exports.delete = async (req, res) => { const { access_token } = req.headers; //const [tokenType, tokenValue] = access_token.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - let { user_id } = req.body; - - checkUserInfo(res, user_id, id); + let { userId } = req.body; + checkUserInfo(res, userId, id); const { affectedRows, insertId } = await repository.delete(cardId); - if (affectedRows > 0) { - res.send({ isSuccess: "true" }); + if (affectedRows == 0) { + res.send({ isSuccess: true }); } else { - res.send({ isSuccess: "false", message: "삭제 실패" }); + res.send({ isSuccess: false, message: "삭제 실패" }); } }; @@ -197,28 +209,25 @@ const checkUserInfo = async (res, userId, id) => { // 유저 정보 확인하기 if (userId != id) { return res.send({ - isSuccess: "false", + isSuccess: false, message: "올바른 토큰 값이 아닙니다.", }); } - // user_id, id 타입 일치 확인 - if (typeof userId !== typeof id) { + // userId, id 타입 일치 확인 + if (userId !== id) { return res.send({ - isSuccess: "false", - message: "타입이 일치하지 않습니다.(user_id 타입은 int형 입니다.)", + isSuccess: false, + message: "타입이 일치하지 않습니다.(userId 타입은 int형 입니다.)", }); } // 유저 정보 가져오기 - const user_info = await userRepogitory.show_user(item.user_id); + const user_info = await userRepository.show_user(userId); if (user_info === null) { return res.send({ - isSuccess: "false", - message: `조회된 값이 없습니다(${errorMessage})`, + isSuccess: false, + message: `조회된 값이 없습니다`, }); } - - // 함수를 통과했다면, 유효한 정보 반환 - return { item, user_info }; }; diff --git a/src/api/cards/repogitory.js b/src/api/cards/repogitory.js index 7386e38..d732d59 100644 --- a/src/api/cards/repogitory.js +++ b/src/api/cards/repogitory.js @@ -2,7 +2,7 @@ const { pool } = require("../../data"); //내 명함 등록 쿼리 exports.create = async ( - user_id, + userId, position, organization, address, @@ -10,9 +10,9 @@ exports.create = async ( tell, email ) => { - const query = `INSERT INTO cards (user_id, position, organization, address, photo, tell, email) VALUES (?,?,?,?,?,?,?);`; + const query = `INSERT INTO cards (userId, position, organization, address, photo, tell, email) VALUES (?,?,?,?,?,?,?);`; return await pool(query, [ - user_id, + userId, position, organization, address, @@ -24,7 +24,8 @@ exports.create = async ( //내 명함 조회 쿼리 exports.show = async ({ cardId, userId }) => { - const query = `SELECT * FROM cards WHERE card_id=? AND user_id =?`; + console.log(cardId, userId); + const query = `SELECT * FROM cards WHERE cardId=? AND userId =?`; let result = await pool(query, [cardId, userId]); return result.length < 0 ? null : result[0]; }; @@ -32,14 +33,14 @@ exports.show = async ({ cardId, userId }) => { //내 명함 전체 조회 exports.show_all = async (id) => { const query = ` - SELECT cards.*, user.phone, user.email, user.name FROM cards JOIN user ON cards.user_id = user.id WHERE user_id=?`; + SELECT cards.*, user.phone, user.email, user.name FROM cards JOIN user ON cards.userId = user.id WHERE userId=?`; const result = await pool(query, [id]); return result.length < 0 ? null : result; }; //다른 명함 조회 쿼리 exports.show_other = async (id) => { - const query = `SELECT * FROM cards WHERE card_id =?`; + const query = `SELECT * FROM cards WHERE cardId =?`; let result = await pool(query, [id]); return result.length < 0 ? null : result[0]; }; @@ -57,7 +58,7 @@ exports.update = async ( const query = ` UPDATE cards SET position=?, organization=?, address=?, photo=?, tell=?, email=? - WHERE card_id = ?; + WHERE cardId = ?; `; return await pool(query, [ @@ -75,7 +76,7 @@ exports.update = async ( exports.delete = async (id) => { const query = ` DELETE FROM cards - WHERE card_id = ?; + WHERE cardId = ?; `; return await pool(query, [id]); From 6fed7235fd2a0e6d95847bf22acdf5195da0ca7b Mon Sep 17 00:00:00 2001 From: ohamin26 Date: Fri, 19 Jan 2024 17:23:31 +0900 Subject: [PATCH 09/10] =?UTF-8?q?=EB=B3=80=EC=88=98=EB=AA=85=20card=5Fid?= =?UTF-8?q?=20user=5Fid=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/cards/controller.js | 78 ++++++++++++++++++------------------- src/api/cards/repogitory.js | 22 +++++------ 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/api/cards/controller.js b/src/api/cards/controller.js index 9496efd..9e74126 100644 --- a/src/api/cards/controller.js +++ b/src/api/cards/controller.js @@ -4,10 +4,10 @@ const jwt = require("jsonwebtoken"); //내 명함 정보 등록 exports.register = async (req, res) => { - let { userId, position, organization, address, tell, email } = req.body; + let { user_id, position, organization, address, tell, email } = req.body; const file = req.files; - if (!userId || !position || !organization || !address || !tell || !email) { + if (!user_id || !position || !organization || !address || !tell || !email) { return res.send({ isSuccess: false, message: "항목 중 null 값이 존재합니다.", @@ -22,14 +22,14 @@ exports.register = async (req, res) => { file["file"][0].filename + time.getTime(); - // jwt 토큰 값 받고 id 값만 분리하기 - db에 userId에 저장하기 위함 + // jwt 토큰 값 받고 id 값만 분리하기 - db에 user_id에 저장하기 위함 const { access_token } = req.headers; //const [tokenType, tokenValue] = authorization.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - checkUserInfo(res, userId, id); + checkUserInfo(res, user_id, id); //db에 저장 정상적으로 저장 시 ok / 실패 시 fail const { affectedRows, insertId } = await repository.create( - (userId = id), + (user_id = id), position, organization, address, @@ -46,30 +46,29 @@ exports.register = async (req, res) => { //내 명함 정보 조회 exports.inquiry = async (req, res) => { - const cardId = req.params.cardId; + const card_id = req.params.cardId; - //userId 가져오기 + //user_id 가져오기 const { access_token } = req.headers; //const [tokenType, tokenValue] = authorization.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - let { userId } = req.body; - - checkUserInfo(res, userId, id); + let { user_id } = req.body; + checkUserInfo(res, user_id, id); // 명함 정보 가져오기 - const item = await repository.show({ cardId, userId: id }); + const item = await repository.show({ card_id, user_id: id }); if (item === null) { return res.send({ isSuccess: false, - message: "조회된 값이 없습니다(cardId나 userId를 확인해주세요)", + message: "조회된 값이 없습니다(card_id나 user_id를 확인해주세요)", }); } //유저 정보 가져오기 - const user_info = await userRepository.show_user(item.userId); + const user_info = await userRepository.show_user(item.user_id); if (user_info == null) { return res.send({ isSuccess: false, - message: "조회된 값이 없습니다(userId를 확인해주세요)", + message: "조회된 값이 없습니다(user_id를 확인해주세요)", }); } @@ -90,12 +89,12 @@ exports.inquiry = async (req, res) => { //내 명함 정보 전체 조회 exports.inquiry_all = async (req, res) => { - //userId 값 가져오기 + //user_id 값 가져오기 const { access_token } = req.headers; - let { userId } = req.body; + let { user_id } = req.body; //const [tokenType, tokenValue] = access_token.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - checkUserInfo(res, userId, id); + checkUserInfo(res, user_id, id); const item_all = await repository.show_all(id); @@ -107,22 +106,22 @@ exports.inquiry_all = async (req, res) => { //다른 유저 명함 정보 조회 exports.inquiry_other = async (req, res) => { - const cardId = req.params.cardId; + const card_id = req.params.cardId; //다른 유저 명함 정보 가져오기 - const item = await repository.show_other(cardId); + const item = await repository.show_other(card_id); if (item == null) { res.send({ isSuccess: false, - message: "조회된 값이 없습니다(cardId를 확인해주세요)", + message: "조회된 값이 없습니다(card_id를 확인해주세요)", }); } //유저 정보 가져오기 - const user_info = await userRepository.show_user(item.userId); + const user_info = await userRepository.show_user(item.user_id); if (user_info == null) { return res.send({ isSuccess: false, - message: "조회된 값이 없습니다(userId를 확인해주세요)", + message: "조회된 값이 없습니다(user_id를 확인해주세요)", }); } @@ -143,21 +142,21 @@ exports.inquiry_other = async (req, res) => { //내 명함 정보 업데이트 exports.update = async (req, res) => { - const cardId = req.params.cardId; + const card_id = req.params.cardId; const { access_token } = req.headers; //const [tokenType, tokenValue] = access_token.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - let { userId, position, organization, address, photo, tell, email } = + let { user_id, position, organization, address, photo, tell, email } = req.body; - checkUserInfo(res, userId, id); + checkUserInfo(res, user_id, id); //명함 정보 가져오기 - const item = await repository.show({ cardId, userId }); + const item = await repository.show({ card_id, user_id }); if (item == null) { res.send({ isSuccess: false, - message: "조회된 값이 없습니다(cardId 또는 userId를 확인해주세요)", + message: "조회된 값이 없습니다(card_id 또는 user_id를 확인해주세요)", }); } @@ -172,7 +171,7 @@ exports.update = async (req, res) => { email ? (email = email) : (email = item.email); const { affectedRows, insertId } = await repository.update( - cardId, + card_id, position, organization, address, @@ -189,41 +188,42 @@ exports.update = async (req, res) => { //내 명함 목록 삭제 exports.delete = async (req, res) => { - const cardId = req.params.cardId; + const card_id = req.params.cardId; const { access_token } = req.headers; //const [tokenType, tokenValue] = access_token.split(" "); const { id } = jwt.verify(access_token, process.env.JWT_KEY); - let { userId } = req.body; - checkUserInfo(res, userId, id); + let { user_id } = req.body; + + checkUserInfo(res, user_id, id); - const { affectedRows, insertId } = await repository.delete(cardId); + const { affectedRows, insertId } = await repository.delete(card_id); - if (affectedRows == 0) { + if (affectedRows > 0) { res.send({ isSuccess: true }); } else { res.send({ isSuccess: false, message: "삭제 실패" }); } }; -const checkUserInfo = async (res, userId, id) => { +const checkUserInfo = async (res, user_id, id) => { // 유저 정보 확인하기 - if (userId != id) { + if (user_id != id) { return res.send({ isSuccess: false, message: "올바른 토큰 값이 아닙니다.", }); } - // userId, id 타입 일치 확인 - if (userId !== id) { + // user_id, id 타입 일치 확인 + if (user_id !== id) { return res.send({ isSuccess: false, - message: "타입이 일치하지 않습니다.(userId 타입은 int형 입니다.)", + message: "타입이 일치하지 않습니다.(user_id 타입은 int형 입니다.)", }); } // 유저 정보 가져오기 - const user_info = await userRepository.show_user(userId); + const user_info = await userRepository.show_user(user_id); if (user_info === null) { return res.send({ isSuccess: false, diff --git a/src/api/cards/repogitory.js b/src/api/cards/repogitory.js index d732d59..1e8145b 100644 --- a/src/api/cards/repogitory.js +++ b/src/api/cards/repogitory.js @@ -2,7 +2,7 @@ const { pool } = require("../../data"); //내 명함 등록 쿼리 exports.create = async ( - userId, + user_id, position, organization, address, @@ -10,9 +10,9 @@ exports.create = async ( tell, email ) => { - const query = `INSERT INTO cards (userId, position, organization, address, photo, tell, email) VALUES (?,?,?,?,?,?,?);`; + const query = `INSERT INTO cards (user_id, position, organization, address, photo, tell, email) VALUES (?,?,?,?,?,?,?);`; return await pool(query, [ - userId, + user_id, position, organization, address, @@ -23,24 +23,24 @@ exports.create = async ( }; //내 명함 조회 쿼리 -exports.show = async ({ cardId, userId }) => { - console.log(cardId, userId); - const query = `SELECT * FROM cards WHERE cardId=? AND userId =?`; - let result = await pool(query, [cardId, userId]); +exports.show = async ({ card_id, user_id }) => { + console.log(card_id, user_id); + const query = `SELECT * FROM cards WHERE card_id=? AND user_id =?`; + let result = await pool(query, [card_id, user_id]); return result.length < 0 ? null : result[0]; }; //내 명함 전체 조회 exports.show_all = async (id) => { const query = ` - SELECT cards.*, user.phone, user.email, user.name FROM cards JOIN user ON cards.userId = user.id WHERE userId=?`; + SELECT cards.*, user.phone, user.email, user.name FROM cards JOIN user ON cards.user_id = user.id WHERE user_id=?`; const result = await pool(query, [id]); return result.length < 0 ? null : result; }; //다른 명함 조회 쿼리 exports.show_other = async (id) => { - const query = `SELECT * FROM cards WHERE cardId =?`; + const query = `SELECT * FROM cards WHERE card_id =?`; let result = await pool(query, [id]); return result.length < 0 ? null : result[0]; }; @@ -58,7 +58,7 @@ exports.update = async ( const query = ` UPDATE cards SET position=?, organization=?, address=?, photo=?, tell=?, email=? - WHERE cardId = ?; + WHERE card_id = ?; `; return await pool(query, [ @@ -76,7 +76,7 @@ exports.update = async ( exports.delete = async (id) => { const query = ` DELETE FROM cards - WHERE cardId = ?; + WHERE card_id = ?; `; return await pool(query, [id]); From fc878441a1e7bec2d0828c30b3b19226e8aa3eea Mon Sep 17 00:00:00 2001 From: ohamin26 Date: Fri, 19 Jan 2024 17:35:41 +0900 Subject: [PATCH 10/10] . --- src/api/cards/repogitory.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/cards/repogitory.js b/src/api/cards/repogitory.js index d2de521..2d82fbd 100644 --- a/src/api/cards/repogitory.js +++ b/src/api/cards/repogitory.js @@ -34,7 +34,7 @@ exports.show = async ({ card_id, user_id }) => { //내 명함 전체 조회 exports.show_all = async (id) => { const query = ` - SELECT cards.*, user.phone, user.email, user.name FROM cards JOIN user ON cards.user_Id = user.id WHERE user_id=?`; + SELECT cards.*, user.phone, user.email, user.name FROM cards JOIN user ON cards.user_id = user.id WHERE user_id=?`; const result = await pool(query, [id]); return result.length < 0 ? null : result; };