Files
weeklyreport/backend/api/auth/reset-password.post.ts

79 lines
2.4 KiB
TypeScript

import { query, execute } from '../../utils/db'
import { hashPassword, generateTempPassword } from '../../utils/password'
import { sendTempPasswordEmail } from '../../utils/email'
import { getClientIp } from '../../utils/ip'
interface ResetPasswordBody {
email: string
name: string
phone?: string
}
/**
* 비밀번호 찾기 (임시 비밀번호 발급)
* POST /api/auth/reset-password
*/
export default defineEventHandler(async (event) => {
const body = await readBody<ResetPasswordBody>(event)
const clientIp = getClientIp(event)
if (!body.email || !body.name) {
throw createError({ statusCode: 400, message: '이메일과 이름을 입력해주세요.' })
}
const emailLower = body.email.toLowerCase()
const nameTrimmed = body.name.trim()
// 사용자 조회 (이메일 + 이름)
let sql = `SELECT * FROM wr_employee_info WHERE employee_email = $1 AND employee_name = $2`
const params: any[] = [emailLower, nameTrimmed]
// 핸드폰 번호도 제공된 경우 추가 검증
if (body.phone) {
const phoneClean = body.phone.replace(/[^0-9]/g, '')
sql += ` AND REPLACE(employee_phone, '-', '') = $3`
params.push(phoneClean)
}
const employees = await query<any>(sql, params)
if (employees.length === 0) {
// 보안상 동일한 메시지 반환
throw createError({ statusCode: 400, message: '입력하신 정보와 일치하는 계정을 찾을 수 없습니다.' })
}
const employee = employees[0]
// 임시 비밀번호 생성
const tempPassword = generateTempPassword()
const hash = await hashPassword(tempPassword)
// 비밀번호 업데이트
await execute(`
UPDATE wr_employee_info
SET password_hash = $1, updated_at = NOW(), updated_ip = $2
WHERE employee_id = $3
`, [hash, clientIp, employee.employee_id])
// 이메일 발송
const emailSent = await sendTempPasswordEmail(
employee.employee_email,
employee.employee_name,
tempPassword
)
if (!emailSent) {
// 이메일 발송 실패해도 비밀번호는 이미 변경됨
console.warn('임시 비밀번호 이메일 발송 실패:', employee.employee_email)
}
// 보안상 이메일 일부만 표시
const maskedEmail = employee.employee_email.replace(/(.{2})(.*)(@.*)/, '$1***$3')
return {
success: true,
message: `임시 비밀번호가 ${maskedEmail}로 발송되었습니다.`,
emailSent
}
})