Browse Source

feat: add field-level error metadata to register endpoint responses

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
feat/registration-page
npmrun 2 weeks ago
parent
commit
9314923f52
  1. 7
      server/api/auth/register.post.ts

7
server/api/auth/register.post.ts

@ -12,13 +12,14 @@ export default defineWrappedResponseHandler(async (event) => {
const parsed = registerSchema.safeParse(body)
if (!parsed.success) {
return R.error(parsed.error.issues[0]?.message || '表单验证失败', null)
const field = parsed.error.issues[0]?.path[0]?.toString() ?? undefined
return R.error(parsed.error.issues[0]?.message || '表单验证失败', { field })
}
const { username, password, captchaToken, captchaText } = parsed.data
if (!verifyCaptcha(captchaToken, captchaText)) {
return R.error('验证码错误或已过期', null)
return R.error('验证码错误或已过期', { field: 'captchaText' })
}
const hashedPassword = await hash(password, 10)
@ -38,7 +39,7 @@ export default defineWrappedResponseHandler(async (event) => {
} catch (err: any) {
const msg = String(err?.message ?? '')
if (msg.toLowerCase().includes('unique') || msg.includes('SQLITE_CONSTRAINT')) {
return R.error('用户名已存在', null)
return R.error('用户名已存在', { field: 'username' })
}
logger.error('Failed to insert user', msg)
return R.error('注册失败,请稍后重试', null)

Loading…
Cancel
Save