Files
TK-FB-Project/api.hyungi.net/models/userModel.js

75 lines
2.0 KiB
JavaScript

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
};