fix: 修复 admin change-password 接口 401 和 CORS 问题
All checks were successful
CI/CD Pipeline / Code Quality (push) Successful in 18s
CI/CD Pipeline / Unit Tests (push) Successful in 45s
CI/CD Pipeline / Build & Deploy Test (push) Has been skipped
CI/CD Pipeline / Build & Deploy Production (push) Successful in 12m21s

- CORS 配置显式放行 PUT/PATCH/DELETE 方法(默认只有 GET/POST/HEAD)
- admin-auth 白名单路径修正 /v1/admin/auth/login → /v1/admin/login
- JWT verify 后手动赋值 request.user,修复 decoded payload 丢失
This commit is contained in:
Wang Zhuoxuan 2026-04-23 22:27:23 +08:00
parent 2c97412c82
commit c70748dde2
2 changed files with 3 additions and 3 deletions

View File

@ -32,7 +32,7 @@ async function main(): Promise<void> {
// ── Plugins ────────────────────────────────────────────────────── // ── Plugins ──────────────────────────────────────────────────────
await app.register(helmet); await app.register(helmet);
await app.register(cors, { origin: true }); await app.register(cors, { origin: true, methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'] });
await app.register(rateLimit, { await app.register(rateLimit, {
max: 60, max: 60,
timeWindow: '1 minute', timeWindow: '1 minute',

View File

@ -18,7 +18,7 @@ async function adminAuthMiddleware(app: FastifyInstance): Promise<void> {
} }
// Skip public admin endpoints // Skip public admin endpoints
const publicPaths = ['/v1/admin/auth', '/v1/admin/auth/login']; const publicPaths = ['/v1/admin/auth', '/v1/admin/login'];
if (publicPaths.some((p) => request.url === p)) { if (publicPaths.some((p) => request.url === p)) {
return; return;
} }
@ -34,7 +34,7 @@ async function adminAuthMiddleware(app: FastifyInstance): Promise<void> {
try { try {
const decoded = app.jwt.verify<JwtPayload>(token); const decoded = app.jwt.verify<JwtPayload>(token);
if (decoded.authType === 'admin') { if (decoded.authType === 'admin') {
// Successfully verified admin JWT - request.jwtVerify() will attach the decoded payload request.user = decoded;
return; return;
} }
} catch { } catch {