Initial commit
This commit is contained in:
42
app/api/register/route.ts
Normal file
42
app/api/register/route.ts
Normal file
@ -0,0 +1,42 @@
|
||||
// app/api/register/route.ts
|
||||
|
||||
import { NextRequest } from "next/server";
|
||||
import { UserService } from "@/lib/services/user.service";
|
||||
import { ApiResponseHandler } from "@/lib/utils/api-response";
|
||||
import { ValidationError } from "@/lib/errors/app-error";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { email, name, password } = body;
|
||||
|
||||
// 验证数据完整性
|
||||
if (!email || !name || !password) {
|
||||
throw new ValidationError("缺少邮箱、姓名或密码");
|
||||
}
|
||||
|
||||
// 验证密码长度
|
||||
if (password.length < 6) {
|
||||
throw new ValidationError("密码长度至少6位");
|
||||
}
|
||||
|
||||
// 使用服务层创建用户
|
||||
const user = await UserService.createUser({
|
||||
email,
|
||||
name,
|
||||
password,
|
||||
});
|
||||
|
||||
// 返回成功响应(不包含敏感信息)
|
||||
const userProfile = {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
createdAt: user.createdAt,
|
||||
};
|
||||
|
||||
return ApiResponseHandler.created(userProfile);
|
||||
} catch (error) {
|
||||
return ApiResponseHandler.error(error as Error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user