import bcrypt from 'bcrypt' import { createError, readBody } from 'h3' import { z } from 'zod' import { getUserByIdWithPassword, updateMemberPassword } from '../../repositories/member-repository' import { requireMemberSession } from '../../utils/member-auth' const updatePasswordSchema = z.object({ currentPassword: z.string().min(1), nextPassword: z.string().min(8).max(32) }) /** * 회원 비밀번호 변경 API * @param {import('h3').H3Event} event - 요청 이벤트 * @returns {Promise<{ ok: true }>} 변경 결과 */ export default defineEventHandler(async (event) => { const session = requireMemberSession(event) const parsedBody = updatePasswordSchema.safeParse(await readBody(event)) if (!parsedBody.success) { throw createError({ statusCode: 400, message: '비밀번호 변경 요청 형식이 올바르지 않습니다.' }) } const user = await getUserByIdWithPassword(session.userId) if (!user) { throw createError({ statusCode: 404, message: '회원 정보를 찾을 수 없습니다.' }) } const isCurrentValid = await bcrypt.compare(parsedBody.data.currentPassword, user.passwordHash) if (!isCurrentValid) { throw createError({ statusCode: 401, message: '현재 비밀번호가 올바르지 않습니다.' }) } const nextHash = await bcrypt.hash(parsedBody.data.nextPassword, 12) await updateMemberPassword({ userId: session.userId, passwordHash: nextHash }) return { ok: true } })