perf: 移除AuthGuard读取用户的操作

This commit is contained in:
2025-12-18 13:51:37 +08:00
parent 91bc9c86fd
commit cd1f6116e8

View File

@@ -2,12 +2,10 @@
import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common';
import { Request } from 'express';
import { UserSessionService } from 'src/auth/service/user-session.service';
import { UserService } from 'src/user/user.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(
private userService: UserService,
private userSessionService: UserSessionService,
) { }
@@ -26,13 +24,12 @@ export class AuthGuard implements CanActivate {
throw new UnauthorizedException('登陆凭证无效,请重新登陆');
}
// 附加 user 到 req
const user = await this.userService.findOne({ userId: session.userId });
if (!user) {
throw new UnauthorizedException('用户不存在');
}
const { userId } = session;
(request as any).user = { ...user, sessionId };
(request as any).user = {
sessionId,
userId,
};
return true;
}
}