mirror of
https://github.com/beilunyang/moemail.git
synced 2026-05-11 18:11:27 +08:00
feat: Enhance registration process with improved validation and error handling
This commit is contained in:
@@ -1,25 +1,28 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { register } from "@/lib/auth"
|
||||
import { authSchema } from "@/lib/validation"
|
||||
import { ZodError } from "zod"
|
||||
import { authSchema, AuthSchema } from "@/lib/validation"
|
||||
|
||||
export const runtime = "edge"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const json = await request.json()
|
||||
const body = authSchema.parse(json)
|
||||
|
||||
await register(body.username, body.password)
|
||||
return Response.json({ success: true })
|
||||
} catch (error) {
|
||||
if (error instanceof ZodError) {
|
||||
return Response.json(
|
||||
{ error: error.errors[0].message },
|
||||
const json = await request.json() as AuthSchema
|
||||
|
||||
try {
|
||||
authSchema.parse(json)
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : "输入格式不正确" },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
return Response.json(
|
||||
const { username, password } = json
|
||||
const user = await register(username, password)
|
||||
|
||||
return NextResponse.json({ user })
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : "注册失败" },
|
||||
{ status: 500 }
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user