73d56bddb8
- growth模块:新增游客承接、行为漏斗、推荐实验和站内生命周期运营 - 额度账本:事务预占、幂等结算、全站并发与字符预算保护 - 聊天与记忆:接入回答反馈、可控记忆和持久化异步提取 - 迁移020与回归:覆盖账号隔离、计数器死锁和真实MySQL并发场景
103 lines
3.3 KiB
TypeScript
103 lines
3.3 KiB
TypeScript
/** 后台/员工登录保持原体系,数字人入口只使用独立 C 端账号。 */
|
|
import { mergeGuest } from "../digital/growth/growth.repository";
|
|
import type { AuthJwtPayload } from "./auth.types";
|
|
import { unauthorized } from "../../shared/http-error";
|
|
import { z } from "zod";
|
|
import { loginDigitalUser } from "../digital/digital.users";
|
|
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) => {
|
|
// C 端开户支持120字符账号,不继承后台50字符限制。
|
|
const body = loginBodySchema
|
|
.extend({
|
|
username: z.string().trim().min(1).max(120),
|
|
guestToken: z.string().max(4000).optional(),
|
|
})
|
|
.parse(request.body);
|
|
const { user, payload } = await loginDigitalUser(body);
|
|
if (body.guestToken) {
|
|
let guest: AuthJwtPayload;
|
|
try {
|
|
guest = app.jwt.verify<AuthJwtPayload>(body.guestToken);
|
|
} catch {
|
|
throw unauthorized("游客凭据已失效,请先退出体验再登录");
|
|
}
|
|
if (
|
|
guest.accountType !== "DIGITAL_GUEST" ||
|
|
guest.scene !== "DIGITAL_APP" ||
|
|
!/^guest:[a-f0-9-]{36}$/.test(guest.sub)
|
|
)
|
|
throw unauthorized("游客凭据不合法");
|
|
await mergeGuest(guest.sub, payload.sub);
|
|
}
|
|
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);
|
|
});
|
|
}
|