All files / models userModel.js

0% Statements 0/27
100% Branches 0/0
0% Functions 0/4
0% Lines 0/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                                                                                                                                                     
const { getDb } = require('../dbPool');
 
// 사용자 조회
const findByUsername = async (username) => {
  try {
    const db = await getDb();
    const [rows] = await db.query(
      'SELECT * FROM users WHERE username = ?', [username]
    );
    return rows[0];
  } catch (err) {
    console.error('DB 오류 - 사용자 조회 실패:', err);
    throw err;
  }
};
 
/**
 * 로그인 실패 횟수를 1 증가시킵니다.
 * @param {number} userId - 사용자 ID
 */
const incrementFailedLoginAttempts = async (userId) => {
    try {
        const db = await getDb();
        await db.execute(
            'UPDATE users SET failed_login_attempts = failed_login_attempts + 1 WHERE user_id = ?',
            [userId]
        );
    } catch (err) {
        console.error('DB 오류 - 로그인 실패 횟수 증가 실패:', err);
        throw err;
    }
};
 
/**
 * 특정 사용자의 계정을 잠급니다.
 * @param {number} userId - 사용자 ID
 */
const lockUserAccount = async (userId) => {
    try {
        const db = await getDb();
        await db.execute(
            'UPDATE users SET locked_until = DATE_ADD(NOW(), INTERVAL 15 MINUTE) WHERE user_id = ?',
            [userId]
        );
    } catch (err) {
        console.error('DB 오류 - 계정 잠금 실패:', err);
        throw err;
    }
};
 
/**
 * 로그인 성공 시, 마지막 로그인 시간을 업데이트하고 실패 횟수와 잠금 상태를 초기화합니다.
 * @param {number} userId - 사용자 ID
 */
const resetLoginAttempts = async (userId) => {
    try {
        const db = await getDb();
        await db.execute(
            'UPDATE users SET last_login_at = NOW(), failed_login_attempts = 0, locked_until = NULL WHERE user_id = ?',
            [userId]
        );
    } catch (err) {
        console.error('DB 오류 - 로그인 상태 초기화 실패:', err);
        throw err;
    }
};
 
 
// 명확한 내보내기
module.exports = {
  findByUsername,
  incrementFailedLoginAttempts,
  lockUserAccount,
  resetLoginAttempts
};