feat: 多功能更新
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
// server/api/auth/register.post.ts
|
||||
import type {
|
||||
IUserRegisterData,
|
||||
IUserRegisterRequest
|
||||
} from "#shared/types";
|
||||
import type { IUserRegisterData, IUserRegisterRequest } from "#shared/types";
|
||||
import {
|
||||
createApiLogger,
|
||||
createErrorResponse,
|
||||
createSuccessResponse,
|
||||
createUpstreamErrorResponse,
|
||||
newApiFetch
|
||||
newApiFetch,
|
||||
toSafeLogError
|
||||
} from "~~/server/utils";
|
||||
|
||||
/**
|
||||
@@ -26,11 +25,28 @@ const REGISTER_FIELDS = [
|
||||
* POST /api/auth/register
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const logger = createApiLogger("auth.register");
|
||||
const requestBody = await readBody<Partial<IUserRegisterRequest> | null>(
|
||||
event
|
||||
);
|
||||
|
||||
if (requestBody !== null && typeof requestBody !== "object") {
|
||||
logger.info("开始", {
|
||||
bodyType: requestBody === null ? "null" : typeof requestBody,
|
||||
hasUsername: typeof requestBody?.username === "string",
|
||||
hasPassword: typeof requestBody?.password === "string",
|
||||
hasEmail: typeof requestBody?.email === "string",
|
||||
hasVerificationCode: typeof requestBody?.verification_code === "string",
|
||||
hasAffCode: typeof requestBody?.aff_code === "string"
|
||||
});
|
||||
|
||||
// 只接受 JSON 对象或 null,避免数组/字符串等无效 body 被透传到上游。
|
||||
if (
|
||||
requestBody !== null &&
|
||||
(typeof requestBody !== "object" || Array.isArray(requestBody))
|
||||
) {
|
||||
logger.warn("请求体格式错误", {
|
||||
bodyType: Array.isArray(requestBody) ? "array" : typeof requestBody
|
||||
});
|
||||
return createErrorResponse(400, "请求体必须是 JSON 对象");
|
||||
}
|
||||
|
||||
@@ -51,8 +67,15 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
});
|
||||
|
||||
logger.done("成功", {
|
||||
hasResult: Boolean(result)
|
||||
});
|
||||
|
||||
return createSuccessResponse(result, "注册成功");
|
||||
} catch (error) {
|
||||
logger.error("失败", {
|
||||
error: toSafeLogError(error)
|
||||
});
|
||||
return createUpstreamErrorResponse(error, "注册失败");
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user