feat: add system configuration platform
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
import { getBootstrapState } from "@/modules/core/server/auth";
|
||||
import { jsonSuccess } from "@/modules/core/server/api";
|
||||
import { jsonFailure, jsonSuccess } from "@/modules/core/server/api";
|
||||
import { ensureSystemDefaults } from "@/modules/core/server/permissions";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(): Promise<Response> {
|
||||
const state = await getBootstrapState();
|
||||
return jsonSuccess("Bootstrap state loaded", state);
|
||||
try {
|
||||
await ensureSystemDefaults();
|
||||
const state = await getBootstrapState();
|
||||
return jsonSuccess("Bootstrap state loaded", state);
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.message : "系统初始化状态读取失败";
|
||||
return jsonFailure(reason, 503);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { createSessionRecord, normalizeEmail, setSessionCookie, toPublicAccount, verifyPassword } from "@/modules/core/server/auth";
|
||||
import { loginWithPassword, setSessionCookie } from "@/modules/core/server/auth";
|
||||
import { jsonFailure, jsonSuccess, readJsonBody } from "@/modules/core/server/api";
|
||||
import { recordAuditLog } from "@/modules/core/server/audit";
|
||||
import { updateData } from "@/modules/core/server/store";
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
@@ -18,45 +17,25 @@ export async function POST(request: Request): Promise<Response> {
|
||||
return jsonFailure("请求数据格式无效");
|
||||
}
|
||||
|
||||
const email = normalizeEmail(readString(body, "email"));
|
||||
const email = readString(body, "email");
|
||||
const password = readString(body, "password");
|
||||
|
||||
if (!email || !password) {
|
||||
return jsonFailure("邮箱和密码不能为空");
|
||||
}
|
||||
|
||||
const result = await updateData((data) => {
|
||||
const account = data.accounts.find((item) => item.email === email);
|
||||
if (!account || account.status !== "active" || !verifyPassword(password, account)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const session = createSessionRecord(account.id);
|
||||
data.sessions.push(session);
|
||||
return { account, session };
|
||||
});
|
||||
|
||||
if (!result) {
|
||||
try {
|
||||
const result = await loginWithPassword({ email, password });
|
||||
await setSessionCookie(result.session.id);
|
||||
return jsonSuccess("登录成功", { account: result.account });
|
||||
} catch (error) {
|
||||
await recordAuditLog({
|
||||
action: "auth.login",
|
||||
action: "account.login",
|
||||
targetType: "account",
|
||||
result: "failure",
|
||||
reason: `登录失败:${email}`,
|
||||
});
|
||||
return jsonFailure("账号或密码错误", 401);
|
||||
const reason = error instanceof Error ? error.message : "登录失败";
|
||||
return jsonFailure(reason, 401);
|
||||
}
|
||||
|
||||
await setSessionCookie(result.session.id);
|
||||
const account = toPublicAccount(result.account);
|
||||
await recordAuditLog({
|
||||
actor: account,
|
||||
action: "auth.login",
|
||||
targetType: "account",
|
||||
targetId: account.id,
|
||||
result: "success",
|
||||
reason: "登录成功",
|
||||
});
|
||||
|
||||
return jsonSuccess("登录成功", { account });
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { createAccountRecord, createSessionRecord, normalizeEmail, setSessionCookie } from "@/modules/core/server/auth";
|
||||
import { createRegistration, setSessionCookie } from "@/modules/core/server/auth";
|
||||
import { jsonFailure, jsonSuccess, readJsonBody } from "@/modules/core/server/api";
|
||||
import { recordAuditLog } from "@/modules/core/server/audit";
|
||||
import { updateData } from "@/modules/core/server/store";
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
@@ -21,6 +19,7 @@ export async function POST(request: Request): Promise<Response> {
|
||||
const name = readString(body, "name");
|
||||
const email = readString(body, "email");
|
||||
const password = readString(body, "password");
|
||||
const organizationId = readString(body, "organizationId");
|
||||
|
||||
if (!name || !email || !password) {
|
||||
return jsonFailure("姓名、邮箱和密码不能为空");
|
||||
@@ -30,59 +29,18 @@ export async function POST(request: Request): Promise<Response> {
|
||||
return jsonFailure("密码至少需要6位");
|
||||
}
|
||||
|
||||
const normalizedEmail = normalizeEmail(email);
|
||||
const created = await updateData((data) => {
|
||||
const exists = data.accounts.some((account) => account.email === normalizedEmail);
|
||||
if (exists) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const account = createAccountRecord({
|
||||
try {
|
||||
const created = await createRegistration({
|
||||
name,
|
||||
email,
|
||||
password,
|
||||
role: "caregiver",
|
||||
organizationId: organizationId || undefined,
|
||||
});
|
||||
const session = createSessionRecord(account.id);
|
||||
|
||||
data.accounts.push(account);
|
||||
data.sessions.push(session);
|
||||
|
||||
return { account, session };
|
||||
});
|
||||
|
||||
if (!created) {
|
||||
return jsonFailure("账号已存在", 409);
|
||||
await setSessionCookie(created.session.id);
|
||||
return jsonSuccess("账号已创建", { account: created.account });
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.message : "账号创建失败";
|
||||
const status = reason === "账号已存在" ? 409 : 500;
|
||||
return jsonFailure(reason, status);
|
||||
}
|
||||
|
||||
await setSessionCookie(created.session.id);
|
||||
await recordAuditLog({
|
||||
actor: {
|
||||
id: created.account.id,
|
||||
name: created.account.name,
|
||||
email: created.account.email,
|
||||
role: created.account.role,
|
||||
status: created.account.status,
|
||||
createdAt: created.account.createdAt,
|
||||
updatedAt: created.account.updatedAt,
|
||||
},
|
||||
action: "account.register",
|
||||
targetType: "account",
|
||||
targetId: created.account.id,
|
||||
result: "success",
|
||||
reason: "创建照护人员账号",
|
||||
});
|
||||
|
||||
return jsonSuccess("账号已创建", {
|
||||
account: {
|
||||
id: created.account.id,
|
||||
name: created.account.name,
|
||||
email: created.account.email,
|
||||
role: created.account.role,
|
||||
status: created.account.status,
|
||||
createdAt: created.account.createdAt,
|
||||
updatedAt: created.account.updatedAt,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import { getCurrentAuthContext } from "@/modules/core/server/auth";
|
||||
import { jsonSuccess } from "@/modules/core/server/api";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(): Promise<Response> {
|
||||
const context = await getCurrentAuthContext();
|
||||
|
||||
return jsonSuccess("Session loaded", {
|
||||
account: context?.account ?? null,
|
||||
organization: context?.organization ?? null,
|
||||
membership: context?.membership ?? null,
|
||||
permissions: context?.permissions ?? [],
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { createAccountRecord, createSessionRecord, setSessionCookie } from "@/modules/core/server/auth";
|
||||
import { setSessionCookie, setupFirstPlatformAdmin } from "@/modules/core/server/auth";
|
||||
import { jsonFailure, jsonSuccess, readJsonBody } from "@/modules/core/server/api";
|
||||
import { recordAuditLog } from "@/modules/core/server/audit";
|
||||
import { updateData } from "@/modules/core/server/store";
|
||||
import { ensureSystemDefaults } from "@/modules/core/server/permissions";
|
||||
|
||||
function readString(source: Record<string, unknown>, key: string): string {
|
||||
const value = source[key];
|
||||
@@ -31,60 +30,14 @@ export async function POST(request: Request): Promise<Response> {
|
||||
return jsonFailure("密码至少需要6位");
|
||||
}
|
||||
|
||||
const created = await updateData((data) => {
|
||||
if (data.accounts.length > 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const account = createAccountRecord({
|
||||
name,
|
||||
email,
|
||||
password,
|
||||
role: "admin",
|
||||
organization,
|
||||
});
|
||||
const session = createSessionRecord(account.id);
|
||||
|
||||
data.accounts.push(account);
|
||||
data.sessions.push(session);
|
||||
|
||||
return { account, session };
|
||||
});
|
||||
|
||||
if (!created) {
|
||||
return jsonFailure("系统已完成初始化", 409);
|
||||
try {
|
||||
await ensureSystemDefaults();
|
||||
const created = await setupFirstPlatformAdmin({ organization, name, email, password });
|
||||
await setSessionCookie(created.session.id);
|
||||
return jsonSuccess("初始化完成", { account: created.account });
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.message : "初始化失败";
|
||||
const status = reason === "系统已完成初始化" ? 409 : 500;
|
||||
return jsonFailure(reason, status);
|
||||
}
|
||||
|
||||
await setSessionCookie(created.session.id);
|
||||
await recordAuditLog({
|
||||
actor: {
|
||||
id: created.account.id,
|
||||
name: created.account.name,
|
||||
email: created.account.email,
|
||||
role: created.account.role,
|
||||
organization: created.account.organization,
|
||||
status: created.account.status,
|
||||
createdAt: created.account.createdAt,
|
||||
updatedAt: created.account.updatedAt,
|
||||
},
|
||||
action: "account.setup",
|
||||
targetType: "account",
|
||||
targetId: created.account.id,
|
||||
result: "success",
|
||||
reason: "初始化管理员账号",
|
||||
});
|
||||
|
||||
return jsonSuccess("初始化完成", {
|
||||
account: {
|
||||
id: created.account.id,
|
||||
name: created.account.name,
|
||||
email: created.account.email,
|
||||
role: created.account.role,
|
||||
organization: created.account.organization,
|
||||
status: created.account.status,
|
||||
createdAt: created.account.createdAt,
|
||||
updatedAt: created.account.updatedAt,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user