From 8ab8eea8dcda775b439d7e3bf45aff8f298c9ba4 Mon Sep 17 00:00:00 2001 From: hyejungg Date: Sun, 5 Mar 2023 23:25:06 +0900 Subject: [PATCH 1/4] =?UTF-8?q?[mod]=20fcm=20=ED=86=A0=ED=81=B0=20?= =?UTF-8?q?=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8=20=ED=95=98=EB=8A=94=20api?= =?UTF-8?q?=20=EC=A3=BC=EC=84=9D=EC=B2=98=EB=A6=AC=20(=EC=B6=94=ED=9B=84?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C=20=EC=98=88=EC=A0=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/userController.js | 24 ++++++++++++------------ src/routes/userRoutes.js | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index e1bc499..a021c9f 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -57,18 +57,18 @@ module.exports = { next(err); } }, - updateUserFCMToken: async function (req, res, next) { - await User.updateFCM(req) - .then(() => { - res.status(StatusCode.OK).json({ - success: true, - message: SuccessMessage.userFcmTokenUpdate, - }); - }) - .catch((err) => { - next(err); - }); - }, + // updateUserFCMToken: async function (req, res, next) { + // await User.updateFCM(req) + // .then(() => { + // res.status(StatusCode.OK).json({ + // success: true, + // message: SuccessMessage.userFcmTokenUpdate, + // }); + // }) + // .catch((err) => { + // next(err); + // }); + // }, selectUserInfo: async function (req, res, next) { await User.selectInfo(req) .then((result) => { diff --git a/src/routes/userRoutes.js b/src/routes/userRoutes.js index 8eb19b3..b1f0dcc 100644 --- a/src/routes/userRoutes.js +++ b/src/routes/userRoutes.js @@ -11,7 +11,7 @@ router.put( multer.upload.single('profile_img'), userController.updateUserInfo, ); -router.put('/fcm', verifyToken, userController.updateUserFCMToken); +// router.put('/fcm', verifyToken, userController.updateUserFCMToken); router.put('/re-passwd', verifyToken, userController.updateUserPassword); router.put( '/push-state/:push', From 89d48b55e045cdebb608a791fecef73698a2c3eb Mon Sep 17 00:00:00 2001 From: hyejungg Date: Sun, 5 Mar 2023 23:25:45 +0900 Subject: [PATCH 2/4] =?UTF-8?q?[mod]=20=EB=A1=9C=EA=B7=B8=EC=95=84?= =?UTF-8?q?=EC=9B=83=20=EC=8B=9C=20fcm=20=ED=86=A0=ED=81=B0=20null=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/authController.js | 10 +++++----- src/models/user.js | 2 +- src/utils/response.js | 1 + 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/controllers/authController.js b/src/controllers/authController.js index 0a7f5eb..fef069a 100644 --- a/src/controllers/authController.js +++ b/src/controllers/authController.js @@ -172,11 +172,11 @@ module.exports = { }, logout: async function (req, res, next) { try { - await expiredRefreshToken(req).then(() => { - return res.status(StatusCode.OK).json({ - success: true, - message: SuccessMessage.logoutSuccess, - }); + await expiredRefreshToken(req); + await User.updateFCM(req); + return res.status(StatusCode.OK).json({ + success: true, + message: SuccessMessage.logoutSuccess, }); } catch (err) { next(err); diff --git a/src/models/user.js b/src/models/user.js index 94084fd..827d509 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -168,7 +168,7 @@ module.exports = { }, updateFCM: async function (req) { const userId = Number(req.decoded); - const fcmToken = req.body.fcm_token; + const fcmToken = null; const sqlUpdate = 'UPDATE users SET fcm_token = ? WHERE user_id = ?'; const params = [fcmToken, userId]; diff --git a/src/utils/response.js b/src/utils/response.js index e8ec2d8..13b92e7 100644 --- a/src/utils/response.js +++ b/src/utils/response.js @@ -114,6 +114,7 @@ const ErrorMessage = { validateEmail: '이미 존재하는 이메일 주소', unValidateUser: '존재하지 않는 유저', checkIDPasswordAgain: '이메일 주소 혹은 비밀번호를 다시 확인', + failedUpdateFcmToken: '로그인 시 사용자 fcm 토큰 변경 실패', sendMailFailed: '새 비밀번호 지정을 위한 메일 전송 실패', unValidateVerificationCode: '유효하지 않은 인증번호.', From 07aab6291e94949c9e5f23ff1aee32a269d5fb49 Mon Sep 17 00:00:00 2001 From: hyejungg Date: Sun, 5 Mar 2023 23:26:16 +0900 Subject: [PATCH 3/4] =?UTF-8?q?[mod]=20=EC=9E=AC=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20=EC=8B=9C=20pushState=20=EC=A0=84=EB=8B=AC=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/authController.js | 1 - src/models/user.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/controllers/authController.js b/src/controllers/authController.js index fef069a..b9e781b 100644 --- a/src/controllers/authController.js +++ b/src/controllers/authController.js @@ -139,7 +139,6 @@ module.exports = { message: SuccessMessage.loginSuccess, data: { token, - pushState: data[0].push_state, tempNickname, }, }); diff --git a/src/models/user.js b/src/models/user.js index 827d509..4190abd 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -47,7 +47,7 @@ module.exports = { const email = req.body.email; const sqlSelect = - 'SELECT user_id, email, nickname, push_state FROM users WHERE email = ? AND is_active = true'; + 'SELECT user_id, email, nickname FROM users WHERE email = ? AND is_active = true'; const [rows] = await db.query(sqlSelect, [email]); From 3789e1efcf56a22ccfac7f25d483b1475c514aed Mon Sep 17 00:00:00 2001 From: hyejungg Date: Sun, 5 Mar 2023 23:26:43 +0900 Subject: [PATCH 4/4] =?UTF-8?q?[mod]=20=ED=9A=8C=EC=9B=90=EA=B0=80?= =?UTF-8?q?=EC=9E=85/=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EC=8B=9C=20fcm=20?= =?UTF-8?q?=ED=86=A0=ED=81=B0=20=EA=B0=92=20=EB=B0=9B=EB=8F=84=EB=A1=9D=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/controllers/authController.js | 4 ++-- src/models/user.js | 31 ++++++++++++++++++++++--------- src/utils/response.js | 1 + 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/controllers/authController.js b/src/controllers/authController.js index b9e781b..c440dbe 100644 --- a/src/controllers/authController.js +++ b/src/controllers/authController.js @@ -53,7 +53,7 @@ module.exports = { signUp: async function (req, res, next) { try { // TODO DTO 만들어서 req.body로 넘기지 않도록 수정하기 (전체적으로) - if (!req.body.email || !req.body.password) { + if (!req.body.email || !req.body.password || !req.body.fcmToken) { throw new BadRequest(ErrorMessage.BadRequestMeg); } @@ -72,7 +72,7 @@ module.exports = { }, signIn: async function (req, res, next) { try { - if (!req.body.email || !req.body.password) { + if (!req.body.email || !req.body.password || !req.body.fcmToken) { throw new BadRequest(ErrorMessage.BadRequestMeg); } await User.signIn(req).then(async (data) => { diff --git a/src/models/user.js b/src/models/user.js index 4190abd..97b0010 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -9,38 +9,51 @@ module.exports = { signUp: async function (req) { const email = req.body.email; const password = req.body.password; + const fcmToken = req.body.fcmToken; const hashPassword = bcrypt.hashSync(password, 10); const sqlInsert = - 'INSERT IGNORE INTO users (email, password) VALUES (?, ?)'; - const params = [email, hashPassword]; + 'INSERT IGNORE INTO users (email, password, fcm_token) VALUES (?, ?, ?)'; + const params = [email, hashPassword, fcmToken]; const [rows] = await db.queryWithTransaction(sqlInsert, params); if (rows.affectedRows < 1) { - throw new NotFound(ErrorMessage.validateEmail); + throw new NotFound(ErrorMessage.existsUserFcmToken); } return rows.insertId; }, signIn: async function (req) { const email = req.body.email; const password = req.body.password; + const fcmToken = req.body.fcmToken; const sqlSelect = - 'SELECT user_id, email, nickname, password, is_active FROM users WHERE email = ?'; - const [rows] = await db.query(sqlSelect, [email]); + 'SELECT user_id, email, nickname, fcm_token, password, is_active FROM users WHERE email = ?'; + const [selectRows] = await db.query(sqlSelect, [email]); - if (rows.affectedRows < 1) { + if (selectRows.affectedRows < 1) { throw new NotFound(ErrorMessage.unValidateUser); } - const checkPassword = bcrypt.compareSync(password, rows[0].password); + const checkPassword = bcrypt.compareSync(password, selectRows[0].password); + + if (selectRows[0].fcm_token !== fcmToken) { + const sqlUpdate = 'UPDATE users SET fcm_token = ? WHERE user_id = ?'; + const params = [fcmToken, selectRows[0].user_id]; + + const [updateRows] = await db.queryWithTransaction(sqlUpdate, params); + + if (updateRows.affectedRows < 1) { + throw new NotFound(ErrorMessage.failedUpdateFcmToken); + } + } return { result: checkPassword, - userId: rows[0].user_id, - nickname: rows[0].nickname, + userId: selectRows[0].user_id, + nickname: selectRows[0].nickname, }; }, restartSignIn: async function (req) { diff --git a/src/utils/response.js b/src/utils/response.js index 13b92e7..6672869 100644 --- a/src/utils/response.js +++ b/src/utils/response.js @@ -111,6 +111,7 @@ const ErrorMessage = { unActiveUser: '탈퇴 처리된 유저', signUpFailed: 'wishboard 앱 회원가입 실패', + existsUserFcmToken: '이미 존재하는 사용자의 fcmToken', validateEmail: '이미 존재하는 이메일 주소', unValidateUser: '존재하지 않는 유저', checkIDPasswordAgain: '이메일 주소 혹은 비밀번호를 다시 확인',