feat: 多功能更新
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
// server/api/auth/login.post.ts
|
||||
import { consola } from "consola";
|
||||
import type { IUserLoginData, IUserLoginRequest } from "#shared/types";
|
||||
import {
|
||||
clearNewApiAuthCookies,
|
||||
createApiLogger,
|
||||
createErrorResponse,
|
||||
createSuccessResponse,
|
||||
createUpstreamErrorResponse,
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
extractCookieValueFromSetCookie,
|
||||
newApiFetchRaw,
|
||||
setNewApiAuthCookies,
|
||||
toSafeLogError,
|
||||
upsertUserSnapshot
|
||||
} from "~~/server/utils";
|
||||
|
||||
@@ -28,6 +29,7 @@ interface INewApiLoginUserData extends IUserLoginData {
|
||||
/** NewAPI 登录结果如果返回额外字段,服务端会在返回前裁剪掉 */
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* 允许从请求体透传给上游的字段列表。
|
||||
* 只有在这里声明过的字段才会被转发,其余字段一律丢弃,防止参数污染。
|
||||
@@ -40,11 +42,24 @@ const LOGIN_FIELDS = ["username", "password"] as const satisfies ReadonlyArray<
|
||||
* POST /api/auth/login
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const logger = createApiLogger("auth.login");
|
||||
const requestBody = await readBody<Partial<IUserLoginRequest> | null>(event);
|
||||
|
||||
logger.info("开始", {
|
||||
bodyType: requestBody === null ? "null" : typeof requestBody,
|
||||
hasUsername: typeof requestBody?.username === "string",
|
||||
hasPassword: typeof requestBody?.password === "string"
|
||||
});
|
||||
|
||||
// 只接受 JSON 对象或 null,避免数组/字符串等无效 body 被透传到上游。
|
||||
if (requestBody !== null && typeof requestBody !== "object") {
|
||||
if (
|
||||
requestBody !== null &&
|
||||
(typeof requestBody !== "object" || Array.isArray(requestBody))
|
||||
) {
|
||||
clearNewApiAuthCookies(event);
|
||||
logger.warn("请求体格式错误", {
|
||||
bodyType: Array.isArray(requestBody) ? "array" : typeof requestBody
|
||||
});
|
||||
return createErrorResponse(400, "请求体必须是 JSON 对象");
|
||||
}
|
||||
|
||||
@@ -70,19 +85,28 @@ export default defineEventHandler(async (event) => {
|
||||
);
|
||||
|
||||
const normalized = normalizeLoginResponse(result._data);
|
||||
const session = extractCookieValueFromSetCookie(result.headers, "session");
|
||||
|
||||
logger.info("上游登录返回", {
|
||||
success: Boolean(normalized.user),
|
||||
hasSession: Boolean(session)
|
||||
});
|
||||
|
||||
if (!normalized.user) {
|
||||
// 上游业务失败时同步清理本地旧登录态,避免继续携带坏 cookie。
|
||||
clearNewApiAuthCookies(event);
|
||||
logger.warn("登录业务失败", {
|
||||
upstreamMessage: normalized.message
|
||||
});
|
||||
return createErrorResponse(1, normalized.message, result._data ?? null);
|
||||
}
|
||||
|
||||
const session = extractCookieValueFromSetCookie(result.headers, "session");
|
||||
if (!session) {
|
||||
// 没有 session 就不能恢复 NewAPI 登录态,必须让前端明确感知失败。
|
||||
clearNewApiAuthCookies(event);
|
||||
consola.error(
|
||||
"/api/auth/login 登录成功但未收到 NewAPI session,请稍后重试"
|
||||
);
|
||||
logger.error("登录成功但未收到 NewAPI session", {
|
||||
userId: normalized.user.id
|
||||
});
|
||||
return createErrorResponse(500, "服务器错误,请稍后重试");
|
||||
}
|
||||
|
||||
@@ -94,7 +118,16 @@ export default defineEventHandler(async (event) => {
|
||||
);
|
||||
|
||||
upsertUserSnapshot(normalized.user).catch((error) => {
|
||||
consola.error("/api/auth/login 保存用户快照失败", error);
|
||||
logger.error("保存用户快照失败", {
|
||||
userId: normalized.user?.id,
|
||||
error: toSafeLogError(error)
|
||||
});
|
||||
});
|
||||
|
||||
logger.done("成功", {
|
||||
userId: normalized.user.id,
|
||||
role: normalized.user.role,
|
||||
status: normalized.user.status
|
||||
});
|
||||
|
||||
return createSuccessResponse<IUserLoginData>(
|
||||
@@ -103,7 +136,9 @@ export default defineEventHandler(async (event) => {
|
||||
);
|
||||
} catch (error) {
|
||||
clearNewApiAuthCookies(event);
|
||||
consola.error("/api/auth/login 登录失败", error);
|
||||
logger.error("失败", {
|
||||
error: toSafeLogError(error)
|
||||
});
|
||||
return createUpstreamErrorResponse(error, "登录失败");
|
||||
}
|
||||
});
|
||||
@@ -112,7 +147,6 @@ export default defineEventHandler(async (event) => {
|
||||
const normalizeLoginResponse = (
|
||||
response: NewApiLoginResponse | undefined
|
||||
): { message: string; user: IUserLoginData | null } => {
|
||||
consola.info("user data", response);
|
||||
if (isUser(response)) {
|
||||
return {
|
||||
message: "登录成功",
|
||||
@@ -121,7 +155,6 @@ const normalizeLoginResponse = (
|
||||
}
|
||||
|
||||
if (!isRecord(response)) {
|
||||
consola.error("/api/auth/login 登录失败:响应不是对象");
|
||||
return {
|
||||
message: "登录失败",
|
||||
user: null
|
||||
@@ -129,11 +162,9 @@ const normalizeLoginResponse = (
|
||||
}
|
||||
|
||||
const wrapped = response as INewApiWrappedLoginResponse;
|
||||
const message =
|
||||
wrapped.message || (wrapped.success ? "登录成功" : "登录失败");
|
||||
const message = wrapped.message || (wrapped.success ? "登录成功" : "登录失败");
|
||||
|
||||
if (wrapped.success !== true || !isUser(wrapped.data)) {
|
||||
consola.error("/api/auth/login 登录失败:上游返回的数据格式不正确");
|
||||
return {
|
||||
message,
|
||||
user: null
|
||||
|
||||
Reference in New Issue
Block a user