Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

회원가입 및 로그인 관련 로직 수정 #245

Merged
merged 4 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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) => {
Expand Down Expand Up @@ -139,7 +139,6 @@ module.exports = {
message: SuccessMessage.loginSuccess,
data: {
token,
pushState: data[0].push_state,
tempNickname,
},
});
Expand Down Expand Up @@ -172,11 +171,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);
Expand Down
24 changes: 12 additions & 12 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
35 changes: 24 additions & 11 deletions src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,58 @@ 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) {
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]);

Expand Down Expand Up @@ -168,7 +181,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];
Expand Down
2 changes: 1 addition & 1 deletion src/routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions src/utils/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ const ErrorMessage = {

unActiveUser: '탈퇴 처리된 유저',
signUpFailed: 'wishboard 앱 회원가입 실패',
existsUserFcmToken: '이미 존재하는 사용자의 fcmToken',
validateEmail: '이미 존재하는 이메일 주소',
unValidateUser: '존재하지 않는 유저',
checkIDPasswordAgain: '이메일 주소 혹은 비밀번호를 다시 확인',
failedUpdateFcmToken: '로그인 시 사용자 fcm 토큰 변경 실패',

sendMailFailed: '새 비밀번호 지정을 위한 메일 전송 실패',
unValidateVerificationCode: '유효하지 않은 인증번호.',
Expand Down