mirror of
https://github.com/beilunyang/moemail.git
synced 2026-09-06 15:56:36 +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 { register } from "@/lib/auth"
|
||||||
import { authSchema } from "@/lib/validation"
|
import { authSchema, AuthSchema } from "@/lib/validation"
|
||||||
import { ZodError } from "zod"
|
|
||||||
|
|
||||||
export const runtime = "edge"
|
export const runtime = "edge"
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const json = await request.json()
|
const json = await request.json() as AuthSchema
|
||||||
const body = authSchema.parse(json)
|
|
||||||
|
try {
|
||||||
await register(body.username, body.password)
|
authSchema.parse(json)
|
||||||
return Response.json({ success: true })
|
} catch (error) {
|
||||||
} catch (error) {
|
return NextResponse.json(
|
||||||
if (error instanceof ZodError) {
|
{ error: error instanceof Error ? error.message : "输入格式不正确" },
|
||||||
return Response.json(
|
|
||||||
{ error: error.errors[0].message },
|
|
||||||
{ status: 400 }
|
{ 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 : "注册失败" },
|
{ error: error instanceof Error ? error.message : "注册失败" },
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ export function LoginForm() {
|
|||||||
const newErrors: FormErrors = {}
|
const newErrors: FormErrors = {}
|
||||||
if (!username) newErrors.username = "请输入用户名"
|
if (!username) newErrors.username = "请输入用户名"
|
||||||
if (!password) newErrors.password = "请输入密码"
|
if (!password) newErrors.password = "请输入密码"
|
||||||
|
if (username.includes('@')) newErrors.username = "用户名不能包含 @ 符号"
|
||||||
if (password && password.length < 8) newErrors.password = "密码长度必须大于等于8位"
|
if (password && password.length < 8) newErrors.password = "密码长度必须大于等于8位"
|
||||||
setErrors(newErrors)
|
setErrors(newErrors)
|
||||||
return Object.keys(newErrors).length === 0
|
return Object.keys(newErrors).length === 0
|
||||||
@@ -48,6 +49,7 @@ export function LoginForm() {
|
|||||||
const newErrors: FormErrors = {}
|
const newErrors: FormErrors = {}
|
||||||
if (!username) newErrors.username = "请输入用户名"
|
if (!username) newErrors.username = "请输入用户名"
|
||||||
if (!password) newErrors.password = "请输入密码"
|
if (!password) newErrors.password = "请输入密码"
|
||||||
|
if (username.includes('@')) newErrors.username = "用户名不能包含 @ 符号"
|
||||||
if (password && password.length < 8) newErrors.password = "密码长度必须大于等于8位"
|
if (password && password.length < 8) newErrors.password = "密码长度必须大于等于8位"
|
||||||
if (!confirmPassword) newErrors.confirmPassword = "请确认密码"
|
if (!confirmPassword) newErrors.confirmPassword = "请确认密码"
|
||||||
if (password !== confirmPassword) newErrors.confirmPassword = "两次输入的密码不一致"
|
if (password !== confirmPassword) newErrors.confirmPassword = "两次输入的密码不一致"
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
|
||||||
export const authSchema = z.object({
|
export const authSchema = z.object({
|
||||||
username: z.string().min(1, "用户名不能为空"),
|
username: z.string()
|
||||||
password: z.string().min(8, "密码长度必须大于等于8位"),
|
.min(1, "用户名不能为空")
|
||||||
|
.max(20, "用户名不能超过20个字符")
|
||||||
|
.regex(/^[a-zA-Z0-9_-]+$/, "用户名只能包含字母、数字、下划线和横杠")
|
||||||
|
.refine(val => !val.includes('@'), "用户名不能是邮箱格式"),
|
||||||
|
password: z.string()
|
||||||
|
.min(8, "密码长度必须大于等于8位")
|
||||||
})
|
})
|
||||||
|
|
||||||
export type AuthSchema = z.infer<typeof authSchema>
|
export type AuthSchema = z.infer<typeof authSchema>
|
||||||
Reference in New Issue
Block a user