Files
sori.studio/server/api/auth/password.put.js
zenn de767095ba v1.10.88 품질·보안 개선과 미디어 카탈로그 database 전환을 반영한다.
세션·로그인 제한, ZIP/업로드 스트리밍, CSP 최소 강제와 Report-Only, Playwright 격리 검증을 추가하고 미디어 목록을 reconcile 후 database 모드로 고정한다.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-22 11:05:40 +09:00

56 lines
1.6 KiB
JavaScript

import bcrypt from 'bcrypt'
import { createError, readBody } from 'h3'
import { z } from 'zod'
import { getUserByIdWithPassword, updateMemberPassword } from '../../repositories/member-repository'
import { clearMemberSession, requireMemberSession } from '../../utils/member-auth'
import { clearAdminSession } from '../../utils/admin-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 = await 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
})
clearMemberSession(event)
clearAdminSession(event)
return { ok: true }
})