00dd5095bb
- digital: 新增私有会话、DeepSeek流式生成、停止与分支、世界书和预设编排 - experiments: 新增账号稳定分组、人工指定、生命周期、预设快照及实验漏斗审计 - migrations/data: 初始化20位成年角色、4本世界书与3种叙事预设 - tests/docs: 补充11项核心回归、市场参考和部署说明
81 lines
2.5 KiB
TypeScript
81 lines
2.5 KiB
TypeScript
/** 现有账号登录与密码接口,数字人入口同时接受管理员与员工。 */
|
|
import { authRepository } from "./auth.repository";
|
|
import type { FastifyInstance } from "fastify";
|
|
import { env } from "../../config/env";
|
|
import { ok } from "../../shared/response";
|
|
import { authGuard } from "./auth.guard";
|
|
import { loginBodySchema, updateOwnPasswordBodySchema } from "./auth.schema";
|
|
import { authService } from "./auth.service";
|
|
|
|
/** 账号登录、当前用户与本人改密接口。 */
|
|
export async function authRoutes(app: FastifyInstance): Promise<void> {
|
|
app.post("/auth/digital/login", async (request) => {
|
|
const body = loginBodySchema.parse(request.body);
|
|
const admin = await authRepository.findByUsername(body.username);
|
|
const { user, payload } = admin
|
|
? await authService.loginManagement(body)
|
|
: await authService.loginEmployee(body);
|
|
return ok({
|
|
token: app.jwt.sign(payload),
|
|
tokenType: "Bearer",
|
|
expiresIn: env.JWT_EXPIRES_IN,
|
|
user,
|
|
});
|
|
});
|
|
|
|
app.post("/auth/login", async (request) => {
|
|
const body = loginBodySchema.parse(request.body);
|
|
const { user, payload } = await authService.loginManagement(body);
|
|
const token = app.jwt.sign(payload);
|
|
|
|
return ok({
|
|
token,
|
|
tokenType: "Bearer",
|
|
expiresIn: env.JWT_EXPIRES_IN,
|
|
user,
|
|
});
|
|
});
|
|
|
|
app.post("/auth/admin/login", async (request) => {
|
|
const body = loginBodySchema.parse(request.body);
|
|
const { user, payload } = await authService.loginManagement(body);
|
|
const token = app.jwt.sign(payload);
|
|
|
|
return ok({
|
|
token,
|
|
tokenType: "Bearer",
|
|
expiresIn: env.JWT_EXPIRES_IN,
|
|
user,
|
|
});
|
|
});
|
|
|
|
app.post("/auth/employee/login", async (request) => {
|
|
const body = loginBodySchema.parse(request.body);
|
|
const { user, payload } = await authService.loginEmployee(body);
|
|
const token = app.jwt.sign(payload);
|
|
|
|
return ok({
|
|
token,
|
|
tokenType: "Bearer",
|
|
expiresIn: env.JWT_EXPIRES_IN,
|
|
user,
|
|
});
|
|
});
|
|
|
|
app.get("/auth/me", { preHandler: authGuard }, async (request) => {
|
|
const user = await authService.getCurrentUser(request.user);
|
|
|
|
return ok(user);
|
|
});
|
|
|
|
app.patch("/auth/me/password", { preHandler: authGuard }, async (request) => {
|
|
const body = updateOwnPasswordBodySchema.parse(request.body);
|
|
const user = await authService.updateOwnPassword(request.user, body, {
|
|
ip: request.ip,
|
|
userAgent: request.headers["user-agent"] ?? null,
|
|
});
|
|
|
|
return ok(user);
|
|
});
|
|
}
|