password.post.ts 816 Bytes
import { z } from 'zod'
import { updateMockSettingsPassword } from '../../../utils/mock-settings'

const passwordPayloadSchema = z.object({
  current: z.string().min(8),
  new: z.string().min(8)
})

export default eventHandler(async (event) => {
  const body = await readBody(event)
  const parsed = passwordPayloadSchema.safeParse(body)

  if (!parsed.success) {
    return {
      success: false,
      errorCode: 'VALIDATION_ERROR',
      message: '密码格式不正确。'
    }
  }

  if (parsed.data.current === parsed.data.new) {
    return {
      success: false,
      errorCode: 'VALIDATION_ERROR',
      message: '新旧密码不能相同。'
    }
  }

  const result = updateMockSettingsPassword(parsed.data.current, parsed.data.new)

  if (!result.success) {
    return result
  }

  return result
})