Skip to content

Commit

Permalink
[add] 하나의 기기에서 여러 계정을 사용할 수 있도록 로그인 일부 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
hyejungg committed Dec 16, 2023
1 parent 6b19193 commit 7d554d1
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
'SELECT user_id, fcm_token FROM users WHERE fcm_token = ?';
const [selectRows] = await db.query(sqlSelect, [fcmToken]);

if (selectRows.affectedRows > 1) {
if (selectRows.length > 1) {
const sqlUpdate = 'UPDATE users SET fcm_token = null WHERE user_id = ?';
const [updateRows] = await db.queryWithTransaction(sqlUpdate, [
selectRows[0].user_id,
Expand Down Expand Up @@ -54,6 +54,26 @@ module.exports = {

const checkPassword = bcrypt.compareSync(password, selectRows[0].password);

// fcm 토큰이 다른 유저에 존재한다면, null 처리
const sqlSelectByFcmToken =
'SELECT user_id, fcm_token FROM users WHERE fcm_token = ?';
const [selectFcmToken] = await db.query(sqlSelectByFcmToken, [fcmToken]);

console.log(selectFcmToken);
console.log(selectFcmToken.length >= 1);

if (selectFcmToken.length >= 1) {
const sqlUpdate = 'UPDATE users SET fcm_token = null WHERE user_id = ?';
const [updateRows] = await db.queryWithTransaction(sqlUpdate, [
selectFcmToken[0].user_id,
]);

if (updateRows.affectedRows < 1) {
throw new NotFound(ErrorMessage.failedUpdateFcmToken);
}
}

// 현재 유저로 fcm 토큰 갱신
if (selectRows[0].fcm_token !== fcmToken) {
const sqlUpdate = 'UPDATE users SET fcm_token = ? WHERE user_id = ?';
const params = [fcmToken, selectRows[0].user_id];
Expand Down

0 comments on commit 7d554d1

Please sign in to comment.