From b6836876b79e8c2d33a27199156969afc0c93b04 Mon Sep 17 00:00:00 2001 From: Capella42 <83754482+Capella42@users.noreply.github.com> Date: Wed, 7 Dec 2022 18:47:13 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9D=B4=EC=83=81=ED=98=81-=EA=B3=BC=EC=A0=9C?= =?UTF-8?q?=20=EC=A0=9C=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 제출합니다 --- middlewares/config/jwtOption.js | 6 ++--- package-lock.json | 2 +- routes/user.js | 47 +++++++++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/middlewares/config/jwtOption.js b/middlewares/config/jwtOption.js index cbbe8e1..0616ff6 100644 --- a/middlewares/config/jwtOption.js +++ b/middlewares/config/jwtOption.js @@ -1,13 +1,13 @@ module.exports = { - secretKey: '임강준현영구', // 원하는 시크릿 + secretKey: 'Capella', // 원하는 시크릿 options: { algorithm: "HS256", // 해싱 알고리즘 expiresIn: "1h", // 토큰 유효 기간 - issuer: "dokdogalmaegi" // 발행자 + issuer: "LeeSangHuck" // 발행자 }, refreshTokenOptions: { algorithm: "HS256", // 해싱 알고리즘 expiresIn: "14d", // 토큰 유효 기간 - issuer: "dokdogalmaegi" // 발행자 + issuer: "LeeSangHuck" // 발행자 } } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 7eba578..e4bf3de 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "ExpressLearn", + "name": "Chat-Back", "version": "1.0.0", "lockfileVersion": 2, "requires": true, diff --git a/routes/user.js b/routes/user.js index 3461acf..345e97d 100644 --- a/routes/user.js +++ b/routes/user.js @@ -114,8 +114,51 @@ router.post('/signIn', async (req, res) => { }); // 1. 임의 User의 name을 바꾸는 API를 구현하시면 됩니다. -router.post('/changeUsername', authUtil, async (req, res) => { - return res.json(util.success('test', 'test', 'test')); +router.post("/changeUsername", authUtil, async (req, res) => { + + const id = req.body; + const changeUsername = req.body; + const user = await User.findOne({ id }); + + try { + if (!id) { + throw USER_ID; + } else if (!changeUsername) { + throw changeUsername; + } + + if (!user) { + throw NOT_EXISTS_USER; + } + + await User.updateOne({ id: user.id }, { $set: { name: changeUsername } }); + + return res.json( + util.success("SUCCESS_CHANGE_USER_NAME", "Success change user name!") + ); + } catch ({ code, message }) { + switch (code) { + case USER_ID.code: + return res.json(util.fail(USER_ID.code, USER_ID.message)); + + case USER_PASSWORD.code: + return res.json(util.fail(USER_PASSWORD.code, USER_PASSWORD.message)); + + case NOT_EXISTS_USER.code: + return res.json( + util.fail(NOT_EXISTS_USER.code, NOT_EXISTS_USER.message) + ); + case IN_KOREAN.code: + return res.json(util.fail(IN_KOREAN.code, IN_KOREAN.message)); + + case EXISTS_ID.code: + return res.json(util.fail(EXISTS_ID.code, EXISTS_ID.message)); + + default: + console.log(message); + return res.json(util.fail(OTHER.code, OTHER.message)); + } + } }); module.exports = router; \ No newline at end of file