- 새로운 DB 스키마(v2) 추가 (테이블명 snake_case, FK 적용) - 룰.md에 API 성능 관리 규칙 추가 - 로그인 관련 로직을 새로운 스키마에 맞게 수정 - Service와 Model의 역할 분리를 명확하게 리팩토링
87 lines
2.4 KiB
JavaScript
87 lines
2.4 KiB
JavaScript
const dbPool = require('../dbPool');
|
|
|
|
// 사용자 조회
|
|
const findByUsername = async (username) => {
|
|
let connection;
|
|
try {
|
|
connection = await dbPool.getConnection();
|
|
const [rows] = await connection.query(
|
|
'SELECT * FROM users WHERE username = ?', [username]
|
|
);
|
|
return rows[0];
|
|
} catch (err) {
|
|
console.error('DB 오류 - 사용자 조회 실패:', err);
|
|
throw err;
|
|
} finally {
|
|
if (connection) connection.release();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 로그인 실패 횟수를 1 증가시킵니다.
|
|
* @param {number} userId - 사용자 ID
|
|
*/
|
|
const incrementFailedLoginAttempts = async (userId) => {
|
|
let connection;
|
|
try {
|
|
connection = await dbPool.getConnection();
|
|
await connection.execute(
|
|
'UPDATE users SET failed_login_attempts = failed_login_attempts + 1 WHERE user_id = ?',
|
|
[userId]
|
|
);
|
|
} catch (err) {
|
|
console.error('DB 오류 - 로그인 실패 횟수 증가 실패:', err);
|
|
throw err;
|
|
} finally {
|
|
if (connection) connection.release();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 특정 사용자의 계정을 잠급니다.
|
|
* @param {number} userId - 사용자 ID
|
|
*/
|
|
const lockUserAccount = async (userId) => {
|
|
let connection;
|
|
try {
|
|
connection = await dbPool.getConnection();
|
|
await connection.execute(
|
|
'UPDATE users SET locked_until = DATE_ADD(NOW(), INTERVAL 15 MINUTE) WHERE user_id = ?',
|
|
[userId]
|
|
);
|
|
} catch (err) {
|
|
console.error('DB 오류 - 계정 잠금 실패:', err);
|
|
throw err;
|
|
} finally {
|
|
if (connection) connection.release();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 로그인 성공 시, 마지막 로그인 시간을 업데이트하고 실패 횟수와 잠금 상태를 초기화합니다.
|
|
* @param {number} userId - 사용자 ID
|
|
*/
|
|
const resetLoginAttempts = async (userId) => {
|
|
let connection;
|
|
try {
|
|
connection = await dbPool.getConnection();
|
|
await connection.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;
|
|
} finally {
|
|
if (connection) connection.release();
|
|
}
|
|
};
|
|
|
|
|
|
// 명확한 내보내기
|
|
module.exports = {
|
|
findByUsername,
|
|
incrementFailedLoginAttempts,
|
|
lockUserAccount,
|
|
resetLoginAttempts
|
|
}; |