问题2(SKU 无法自动合并,GBIU017 案例)四处叠加根因修复: - 人工标签旧词「热转印」被封闭词表静默踢出合并矩阵 → CRAFT_TAG_ALIASES 别名归一为「烫画」(矩阵归因与 CUSTOM 标签同路径生效) - updateTags 缺任一定价维度组即整链掉出矩阵且人工接管后永不恢复 → 缺啥补啥(按链接名派生补齐,人工勾选值不动,响应带 filledDimensionTags) - autoGroup 只建新族从不并入已有族(产生 USIU005-2 类碎片)→ 并入已有 autoManaged 同款族优先,无匹配才新建;同款仅人工锁定族则跳过并报告 - 名称回退分组键含物流备注,包邮/不包邮永不同组 → 新增族语义键 familyNameKey(国家+品名+SKU),api/admin 两侧同构,合并默认勾选随之修复 附加: - updateTags 后未归族链接自动并入匹配族(attachToMatchingFamily,响应带 attachedFamilyId) - 整理新增碎片族合并 consolidateFragments:纯碎片族并入带商品族并删除 (保公开 goodId=族ID 稳定),带商品/覆盖价/人工锁定进人工复审报告 - admin:保存标签提示补齐明细,整理完成消息含并入/碎片合并/待人工数 问题1(后台频繁 Unauthorized 掉线): - refresh cookie path '/auth' 与代理前缀(/api、/v2-api)不匹配导致浏览器 永远带不上 refresh cookie → 两 cookie path 统一为 '/' - v2 构建基址带尾斜杠 + 手工拼接产生 /v2-api//auth/refresh 双斜杠 404 → request.ts 规范拼接 测试:api jest 187/187、admin vitest 22/22、双侧 tsc 0 错误; public.service 夹具改为自包含(不依赖共享库既有数据)。
134 lines
4.6 KiB
TypeScript
134 lines
4.6 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Post,
|
|
Req,
|
|
Res,
|
|
UnauthorizedException,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import { Throttle } from '@nestjs/throttler';
|
|
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
|
import { Request, Response } from 'express';
|
|
import { AuthService } from './auth.service';
|
|
import { JwtAuthGuard } from './guards/jwt-auth.guard';
|
|
import { LoginDto } from './dto/login.dto';
|
|
import { RegisterDto } from './dto/register.dto';
|
|
import { LoginResponseDto, UserPublicDto } from './dto/auth-response.dto';
|
|
import type { AuthenticatedUser } from './strategies/jwt.strategy';
|
|
|
|
const ACCESS_TOKEN_COOKIE = 'ir_at';
|
|
const REFRESH_TOKEN_COOKIE = 'ir_rt';
|
|
const isProd = process.env.NODE_ENV === 'production';
|
|
|
|
@ApiTags('auth')
|
|
@Controller('auth')
|
|
export class AuthController {
|
|
constructor(private readonly authService: AuthService) {}
|
|
|
|
@Post('register')
|
|
@HttpCode(HttpStatus.CREATED)
|
|
@Throttle({ default: { limit: 5, ttl: 60_000 } })
|
|
@ApiOperation({ summary: 'Register the first admin user (bootstrap only)' })
|
|
@ApiResponse({ status: 201, type: UserPublicDto })
|
|
@ApiResponse({ status: 409, description: 'Username already exists' })
|
|
@ApiResponse({ status: 403, description: 'Registration is disabled once a user exists' })
|
|
register(@Body() dto: RegisterDto): Promise<UserPublicDto> {
|
|
return this.authService.register(dto) as unknown as Promise<UserPublicDto>;
|
|
}
|
|
|
|
@Post('login')
|
|
@HttpCode(HttpStatus.OK)
|
|
@Throttle({ default: { limit: 5, ttl: 60_000 } })
|
|
@ApiOperation({ summary: 'Login and obtain access + refresh tokens' })
|
|
@ApiResponse({ status: 200, type: LoginResponseDto })
|
|
@ApiResponse({ status: 401, description: 'Invalid credentials' })
|
|
async login(
|
|
@Body() dto: LoginDto,
|
|
@Res({ passthrough: true }) res: Response,
|
|
): Promise<LoginResponseDto> {
|
|
const result = await this.authService.login(dto);
|
|
// HttpOnly cookies are the primary session channel for the admin SPA
|
|
// (XSS cannot read them). The access token is also returned in the
|
|
// body for non-browser API clients.
|
|
res.cookie(ACCESS_TOKEN_COOKIE, result.accessToken, {
|
|
httpOnly: true,
|
|
sameSite: 'lax',
|
|
secure: isProd,
|
|
path: '/',
|
|
});
|
|
res.cookie(REFRESH_TOKEN_COOKIE, result.refreshToken, {
|
|
httpOnly: true,
|
|
sameSite: 'lax',
|
|
secure: isProd,
|
|
// 浏览器实际请求路径带代理前缀(/api/auth/*、/v2-api/auth/*),cookie path
|
|
// 必须用 '/' 才能命中;否则 refresh cookie 永远带不上 → 访问令牌一过期就掉线
|
|
path: '/',
|
|
});
|
|
// The refresh token deliberately stays HttpOnly-only.
|
|
return {
|
|
accessToken: result.accessToken,
|
|
user: result.user,
|
|
} as unknown as LoginResponseDto;
|
|
}
|
|
|
|
@Post('refresh')
|
|
@HttpCode(HttpStatus.OK)
|
|
@Throttle({ default: { limit: 10, ttl: 60_000 } })
|
|
@ApiOperation({ summary: 'Rotate the refresh token cookie' })
|
|
@ApiResponse({ status: 200, type: LoginResponseDto })
|
|
@ApiResponse({ status: 401, description: 'Invalid refresh token' })
|
|
async refresh(
|
|
@Req() req: Request,
|
|
@Res({ passthrough: true }) res: Response,
|
|
): Promise<LoginResponseDto> {
|
|
const token = req.cookies?.[REFRESH_TOKEN_COOKIE];
|
|
if (!token) {
|
|
res.clearCookie(REFRESH_TOKEN_COOKIE, { path: '/' });
|
|
throw new UnauthorizedException('Missing refresh token');
|
|
}
|
|
const result = await this.authService.refresh(token);
|
|
res.cookie(ACCESS_TOKEN_COOKIE, result.accessToken, {
|
|
httpOnly: true,
|
|
sameSite: 'lax',
|
|
secure: isProd,
|
|
path: '/',
|
|
});
|
|
res.cookie(REFRESH_TOKEN_COOKIE, result.refreshToken, {
|
|
httpOnly: true,
|
|
sameSite: 'lax',
|
|
secure: isProd,
|
|
path: '/',
|
|
});
|
|
return {
|
|
accessToken: result.accessToken,
|
|
user: result.user,
|
|
} as unknown as LoginResponseDto;
|
|
}
|
|
|
|
@Get('me')
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiOperation({ summary: 'Current authenticated user' })
|
|
@ApiResponse({ status: 200, type: UserPublicDto })
|
|
me(@Req() req: Request & { user: AuthenticatedUser }): Promise<UserPublicDto> {
|
|
return this.authService.me(req.user.id) as unknown as Promise<UserPublicDto>;
|
|
}
|
|
|
|
@Post('logout')
|
|
@HttpCode(HttpStatus.OK)
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiOperation({ summary: 'Revoke all tokens of the current user' })
|
|
async logout(
|
|
@Req() req: Request & { user: AuthenticatedUser },
|
|
@Res({ passthrough: true }) res: Response,
|
|
): Promise<{ success: true }> {
|
|
await this.authService.logout(req.user.id);
|
|
res.clearCookie(ACCESS_TOKEN_COOKIE, { path: '/' });
|
|
res.clearCookie(REFRESH_TOKEN_COOKIE, { path: '/' });
|
|
return { success: true };
|
|
}
|
|
}
|