From cd1f6116e8975f2407be13141e33dfbd282f24c9 Mon Sep 17 00:00:00 2001 From: tone Date: Thu, 18 Dec 2025 13:51:37 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E7=A7=BB=E9=99=A4AuthGuard=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E7=94=A8=E6=88=B7=E7=9A=84=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/backend/src/auth/guards/auth.guard.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/apps/backend/src/auth/guards/auth.guard.ts b/apps/backend/src/auth/guards/auth.guard.ts index cef08cb..e97af04 100644 --- a/apps/backend/src/auth/guards/auth.guard.ts +++ b/apps/backend/src/auth/guards/auth.guard.ts @@ -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; } } \ No newline at end of file