import { ForbiddenException, UnauthorizedException } from "@nestjs/common"; import type { FastifyRequest } from "fastify"; import { readIdentity } from "../../auth.js"; export type AdminIdentity = { subject: string; brandId: string; role: "admin"; tokenVersion: number; }; export function requireAdminIdentity(request: FastifyRequest): AdminIdentity { const token = request.headers.authorization?.replace(/^Bearer\s+/i, ""); if (!token) throw new UnauthorizedException("缺少管理员登录态"); let identity; try { identity = readIdentity(token); } catch { throw new UnauthorizedException("管理员登录态无效或已过期"); } if (identity.role !== "admin") throw new ForbiddenException("无管理员权限"); return identity as AdminIdentity; }